Python3 Selenium Webdriver 在初始化期间超时



以下脚本:

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:UsersusernameAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverfirefoxwebdriver.py", line 152, in __init__
keep_alive=True)
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 254, in execute
response = self.command_executor.execute(driver_command, params)
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverremoteremote_connection.py", line 464, in execute
return self._request(command_info[0], url, body=data)
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverremoteremote_connection.py", line 488, in _request
resp = self._conn.getresponse()
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libhttpclient.py", line 1334, in getresponse
response.begin()
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libhttpclient.py", line 300, in begin
version, status, reason = self._read_status()
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libhttpclient.py", line 260, in _read_status
line_tmp = self.fp.readline(_MAXLINE + 1)
File "C:UsersusernameAppDataLocalProgramsPythonPython36-32libsocket.py", line 586, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out

Chrome也是如此(显然,除了...\webdriver\chrome\webdriver.py(。这种行为是非常一致的,尽管在极少数情况下,重新尝试在同一 Python 会话中打开 Web 驱动程序将使 Selenium 成功打开浏览器,并且(据我所知(从那里开始正确运行。

在我的 Linux 机器上没有观察到这种行为,它位于同一网络上;任何打开 Web 驱动程序的尝试都可以正常工作。

我很困惑,我通过 Google 发现的所有套接字超时问题都与尝试访问网页有关,而不仅仅是创建新的 Web 驱动程序对象。我可以提供解决此问题所需的任何其他信息。

这是您问题的答案 -

当您使用Selenium 3.4.3chromedriver v2.30geckodriver v0.17.0Google Chrome 59.0Mozilla Firefox 53.0Python 3.6.1时,您可以考虑以下选项:

铬:

要初始化chromedriver,您可以考虑通过executable_path参数提及chromedriver的绝对路径,并另外指定/使用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,您可以考虑通过executable_path参数提及geckodriver的绝对路径,并另外指定/使用firefox_binary参数来配置所需的浏览器,如下所示:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
binary = FirefoxBinary('C:\Program Files\Mozilla Firefox\firefox.exe')
caps = DesiredCapabilities().FIREFOX
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\Utility\BrowserDrivers\geckodriver.exe")
driver.get('https://stackoverflow.com')

让我知道这是否回答了您的问题。

相关内容

  • 没有找到相关文章

最新更新