以下脚本:
from selenium import webdriver
wd = webdriver.Firefox()
或
from selenium import webdriver
wd = webdriver.Chrome()
几乎总是在我的Windows 7机器上失败,产生以下堆栈跟踪:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
keep_alive=True)
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 254, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 464, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 488, in _request
resp = self._conn.getresponse()
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1334, in getresponse
response.begin()
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 300, in begin
version, status, reason = self._read_status()
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 260, in _read_status
line_tmp = self.fp.readline(_MAXLINE + 1)
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 586, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out
Chrome也是如此(显然除了... \ webdriver \ chrome \ webdriver.py)。这种行为是非常一致的,尽管在极少数情况下重新尝试在同一个Python会话中打开一个webdriver将允许Selenium成功打开浏览器,并且(我已经能够告诉)从那里开始正确运行。 / p>
在我的Linux计算机上没有观察到此行为,该计算机位于同一网络上;任何打开webdriver的尝试都能正常工作。
我很难过,我通过Google找到的所有套接字超时问题都与尝试访问网页有关,而不仅仅是创建一个新的webdriver对象。我可以提供解决此问题所需的任何其他信息。
答案 0 :(得分:0)
以下是您的问题的答案 -
当您使用Selenium 3.4.3
,chromedriver v2.30
,geckodriver v0.17.0
,Google Chrome 59.0
和Mozilla Firefox 53.0
到Python 3.6.1
时,您可以考虑以下选项:< / p>
要初始化chromedriver
,您可以考虑提及chromedriver
到 executable_path
参数的绝对路径,并另外指定/使用 {{1配置所需浏览器属性的参数如下:
chrome_options
要初始化from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get("https://www.google.co.in")
,您可以考虑提及geckodriver
到 geckodriver
参数的绝对路径,并另外指定/使用 {{1配置所需浏览器的参数如下:
executable_path
如果这回答你的问题,请告诉我。