英文:
I have two identical projects but I can run Selenium webdriver in only one of them
问题
我正在学习Selenium,我遇到了一个问题,其中我的一个项目可以启动浏览器,但另一个项目会抛出错误。它们现在都有相同的代码,都将webdriver复制到了项目文件夹中。如果我保留这行代码不变:
driver = webdriver.Chrome()
我会得到以下错误:
selenium.common.exceptions.WebDriverException: 消息:'chromedriver'
可执行文件需要在PATH中。请参见
https://sites.google.com/a/chromium.org/chromedriver/home
如果我将它更改为这样:
driver = webdriver.Chrome(r"C:\Users\user\PycharmProjects\Sel\chromedriver.exe")
我会得到以下错误:
ValueError: 连接的超时值为<object object at
0x0000022461CC8720>,但必须是int、float或None。
我不明白为什么会发生这种情况,这两个项目都是相同的,以相同的名称配置,唯一的区别是。到目前为止,整个代码看起来像这样:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(r"C:\Users\user\PycharmProjects\Sel\chromedriver.exe")
driver.get('https://the-internet.herokuapp.com/drag_and_drop')
driver.implicitly_wait(10)
更新
我可以百分之百确定我正在使用最新的Selenium,如下图所示,根据这张图片
英文:
I am learnig Selenium and I encountered a problem where one of my projects can lanuch a browser but the other one throws errors. Both of them have the same code right now, bot have the webdriver copied to project folder. If I leave this line like it is:
driver = webdriver.Chrome()
I get the following error:
> selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
> executable needs to be in PATH. Please see
> https://sites.google.com/a/chromium.org/chromedriver/home
If I change it to this:
driver = webdriver.Chrome(r"C:\Users\user\PycharmProjects\Sel\chromedriver.exe")
I get the following error:
> ValueError: Timeout value connect was <object object at
> 0x0000022461CC8720>, but it must be an int, float or None.
I don't understand why is it happening, both of these projects are the same, configured in the same name, the only difference. So far the whole code looks like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(r"C:\Users\user\PycharmProjects\Sel\chromedriver.exe")
driver.get('https://the-internet.herokuapp.com/drag_and_drop')
driver.implicitly_wait(10)
Update
I am 100% sure I am using latest Selenium, as per the image below, as per this image
答案1
得分: 0
看起来你正在使用较旧版本的Selenium。升级到最新版本的Selenium(4.11.0
),然后你就不需要指定chromedriver.exe
的路径了。Selenium会在内部下载并管理驱动程序。
代码可以简单如下:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
获取更多信息,可以查看以下几个有用的答案:
更新: 如果你正在使用最新的Selenium,那么以下这行代码不正确:
driver = webdriver.Chrome(r"C:\Users\user\PycharmProjects\Sel\chromedriver.exe")
你需要使用Service类来配置驱动程序的路径,如下所示:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service("C:/Users/user/PycharmProjects/Sel/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://www.google.com")
英文:
Looks like you are using older version of Selenium. Upgrade to the latest version of selenium(4.11.0
), then you don't have to specify the path of chromedriver.exe
. Selenium will internally download and manage the drivers for you.
Code can be as simple as:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
For more infor, few useful answers:
UPDATE: If you are using latest selenium, then below line is not correct:
driver = webdriver.Chrome(r"C:\Users\user\PycharmProjects\Sel\chromedriver.exe")
You need to use Service class to configure the driver path, as below:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service("C:/Users/user/PycharmProjects/Sel/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://www.google.com")
答案2
得分: 0
给定您正在使用Selenium的版本为_v4.11.2_,您不再需要传递ChromeDriver二进制文件的绝对路径,因为当检测不到PATH上的浏览器驱动程序或未使用第三方驱动程序管理器时,Selenium Manager已完全集成,将自动透明地被Selenium绑定调用。
您只需要确保:
-
从PATH中删除_ChromeDriver_:
C:\Users\user\PycharmProjects\Sel\
-
系统_PATH_变量不包含以下条目:
C:\Users\user\PycharmProjects\Sel\
然后您可以简单地使用以下代码行:
driver = webdriver.Chrome()
英文:
Given the image as you are on Selenium v4.11.2 you no more need to pass the absolute path of the ChromeDriver binary, as Selenium Manager being fully integrated is invoked transparently by the Selenium bindings when no browser driver is detected on the PATH or no third party driver manager is being used.
You just need to ensure that:
-
The ChromeDriver is removed from the path:
C:\Users\user\PycharmProjects\Sel\
-
The system PATH variable doesn't contains the following entry:
C:\Users\user\PycharmProjects\Sel\
and you can simply use the line of code:
driver = webdriver.Chrome()
答案3
得分: 0
我昨天遇到了相同的错误。如果是相同的情况,我可以通过直接编辑Lib\site-packages\selenium\webdriver\remote\remote_connection.py
文件来修复它。
将文件中的3个出现的_timeout = socket._GLOBAL_DEFAULT_TIMEOUT
替换为_timeout = urllib3.util.timeout._DEFAULT_TIMEOUT
。
我认为这与Jenkins中的这个bug有关:
https://bugs.launchpad.net/python-jenkins/+bug/2018567
这个问题出现在v3.141版本中,但我可以看到在该模块内设置默认超时的代码在v4版本中没有改变。
另请参阅:
英文:
I ran into the same error yesterday. If it is the same, I was able to fix it by directly editing Lib\site-packages\selenium\webdriver\remote\remote_connection.py
Replace the 3 occurrences of _timeout = socket._GLOBAL_DEFAULT_TIMEOUT
with _timeout = urllib3.util.timeout._DEFAULT_TIMEOUT
I think it's related to this bug in Jenkins
https://bugs.launchpad.net/python-jenkins/+bug/2018567
This was in v3.141, but I can see the code for setting the default timeout within that module hasn't changed in v4.
See also
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论