我正在尝试用for循环编写一个程序,为此,我需要在循环之前声明Selenium。每次我试着做这件事,我都会出错。就像循环不识别driver.get部件一样。
以下是代码:
from selenium import webdriver
import time
import csv
with open('smlxl.csv', 'r') as myFile:
myReader = csv.reader(myFile)
myList = list(myReader)
driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\chromedriver.exe')
for n in range(1,1000,4):
barcode = (myList[n][24])
url = ("https://www.google.com/search?q=" + barcode)
driver.get(url)
print(n)
n = (n+4)
time.sleep(3)
driver.close()
我所犯的错误:
C:\Users\DELL\PycharmProjects\pythonProject3\erro.py:10: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\chromedriver.exe')
1
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\pythonProject3\erro.py", line 16, in <module>
driver.get(url)
File "C:\Users\DELL\PycharmProjects\pythonProject3\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 447, in get
self.execute(Command.GET, {'url': url})
File "C:\Users\DELL\PycharmProjects\pythonProject3\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:\Users\DELL\PycharmProjects\pythonProject3\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
Stacktrace:
Backtrace:
Ordinal0 [0x00326463+2188387]
Ordinal0 [0x002BE461+1762401]
Ordinal0 [0x001D3C40+801856]
Ordinal0 [0x001F68BD+944317]
Ordinal0 [0x001F7AD6+948950]
GetHandleVerifier [0x005C71F2+2712546]
GetHandleVerifier [0x005B886D+2652765]
GetHandleVerifier [0x003B002A+520730]
GetHandleVerifier [0x003AEE06+516086]
Ordinal0 [0x002C468B+1787531]
Ordinal0 [0x002C8E88+1805960]
Ordinal0 [0x002C8F75+1806197]
Ordinal0 [0x002D1DF1+1842673]
BaseThreadInitThunk [0x7587FA29+25]
RtlGetAppContainerNamedObjectPath [0x770D7A9E+286]
RtlGetAppContainerNamedObjectPath [0x770D7A6E+238]
Process finished with exit code 1
谢谢你的帮助
发布于 2022-07-22 10:34:56
必须将driver.close()
放置在for循环之外。
发布于 2022-07-22 09:42:27
这些错误信息..。
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
...implies表示上一次会话没有被成功地杀死/移除/消除。
解决方案
你需要替换:
driver.close()
通过以下方式:
driver.quit()
参考文献
您可以在以下网站找到几个相关的详细讨论:
https://stackoverflow.com/questions/73078102
复制相似问题