Selenium不需要ChromeDriver?

huangapple go评论72阅读模式
英文:

Selenium not requiring ChromeDriver?

问题

我在Python中制作了一个简单的Selenium脚本。即使我没有指定ChromeDriver的位置(据我所知,我甚至没有安装ChromeDriver),这个脚本对我来说运行得很完美。我看到的所有其他简单的Selenium程序示例都要求用户指定ChromeDriver的位置。

为什么这里不需要ChromeDriver?

英文:

I made a simple Selenium script below in Python. This script runs perfectly for me, even though I don't specify the location of ChromeDriver (and as far as I can tell, I don't even have ChromeDriver installed). All other examples I've seen of simple Selenium programs require the user to specify the location of ChromeDriver.

Why does it not require ChromeDriver here?

from selenium import webdriver

website_url = 'https://microsoft.com'
driver = webdriver.Chrome()
driver.get(website_url)
driver.quit()

答案1

得分: 1

selenium 版本 4.10.0 起,驱动程序管理器已完全集成,将在需要时静默下载驱动程序。(例如,在Mac/Linux上,如果未在PATH中找到驱动程序,它们会自动下载到“~/.cache/selenium”文件夹中。)稍早版本的 selenium 有一个驱动程序管理器的测试版。

现在,如果你想指定路径,可以通过 service 参数来实现:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service(executable_path="PATH_TO_DRIVER")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()

但现在指定 executable_path 是可选的(必须通过 service 参数来完成)。

英文:

As of selenium 4.10.0 the driver manager is fully integrated, and will silently download drivers as needed. (Eg. On Mac/Linux, drivers are automatically downloaded to the ~/.cache/selenium folder if not found on the PATH.) Slightly earlier versions of selenium had a beta of the driver manager.

Optionally now, if you want to specify a PATH, you can do so via the service arg:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service(executable_path="PATH_TO_DRIVER")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()

But specifying the executable_path is optional now (and must be done via the service arg).

答案2

得分: 0

No more mandatory.

使用 Selenium v10.x 及更高版本,您无需通过 Service 对象或 executable_path 键显式指定 ChromeDriver 的位置。

原因是,如果根据您系统的 PATH 设置未找到 ChromeDriver,它将自动通过新的 Selenium Driver Manager 进行下载,该管理器现在完全集成在 Selenium 中。

  • 最小代码块:

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("https://www.google.com/")
    
  • 控制台输出:

    DevTools listening on ws://127.0.0.1:49197/devtools/browser/07e3d01a-32db-482a-a1e0-dc489efcc03c
    

tl; dr

参考资料:

英文:

No more mandatory.

Using Selenium v10.x and onwards you don't need to explicitly mention the ChromeDriver location through Service object or executable_path key.

The reason behind, incase ChromeDriver is not found as per your system's PATH settings it will automatically get downloaded using the new Selenium Driver Manager which is fully integrated within Selenium now.

  • Minimum code block:

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("https://www.google.com/")
    
  • Console Output:

    DevTools listening on ws://127.0.0.1:49197/devtools/browser/07e3d01a-32db-482a-a1e0-dc489efcc03c
    

tl; dr

References:

huangapple
  • 本文由 发表于 2023年6月15日 04:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定