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:\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 成功打开浏览器,并且(据我所知)从那里开始正确运行。

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

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

德班扬B

这是您问题的答案-

当你一起工作Selenium 3.4.3chromedriver v2.30geckodriver v0.17.0Google Chrome 59.0Mozilla Firefox 53.0通过Python 3.6.1您可以考虑以下选项:

铬合金 :

要初始化,chromedriver您可以考虑提及chromedriverthroughexecutable_path参数的绝对路径,并另外指定/使用该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您可以考虑提及geckodriverthroughexecutable_path参数的绝对路径,并另外指定/使用该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')

如果这能回答您的问题,请告诉我。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Selenium webdriver超时异常

初始化Selenium WebDriver时如何解决python-Selenium错误“连接被拒绝”?

Urllib3-Selenium ConnectionResetError-WinError 10054-初始化webdriver.Chrome

如何设置Selenium Python WebDriver默认超时?

无法通过python3启动Selenium Safari WebDriver

Selenium Webdriver连接超时

StaleElementReferenceException Selenium WebDriver Python

Selenium Webdriver Python storeTextPresent

如何设置Selenium WebDriver超时?

Python-Selenium Webdriver超时异常错误2

Chrome Webdriver Selenium内存错误-Python 3

Selenium Webdriver c#:获取“路径不是合法形式。” 在初始化FirefoxProfile时

Python Selenium:处理Webdriver异常

使用Java与Python的Selenium Webdriver

Python 3.5 Selenium Webdriver IE

在Selenium Webdriver(Python)中滚动

Selenium Webdriver 获取元素(Python)

运行自动化测试时 Selenium Webdriver 连接超时

Selenium Webdriver上的超时默认值

具有Java NoSuchElementException超时的Selenium WebDriver

使用 Selenium 进行 WebScraping 期间的 Selenium WebDriver/ChromeDriver 错误

在函数中调用驱动程序时,如何在 Python3 中保持 Selenium Webdriver 浏览器打开?

在不启动 webdriver 的情况下设置 python selenium webdriver

右键单击某个元素时,出现“未初始化的常量 Selenium::WebDriver::DriverExtensions::HasInputDevices (NameError)”错误

Selepy Selenium,webdriver无法实例化

Selenium Webdriver java 自动化窗口

使用Selenium Webdriver Python上传文件

如何获取早期版本的Python Selenium Webdriver

Python Selenium Webdriver-代理:查询参数