TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' in Selenium Python

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

TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' in Selenium Python

问题

  1. 来自 selenium import webdriver
  2. 来自 selenium.webdriver.chrome.options import Options
  3. option = webdriver.ChromeOptions()
  4. driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=option)
  5. driver.get('https://www.google.com/')

输出:

  1. WebDriver.__init__() 收到了一个意外的关键字参数 'executable_path'

我试图创建一个用于登录网站的脚本。当我尝试运行此脚本时,它给我返回了这个错误:
WebDriver.__init__() 收到了一个意外的关键字参数 'executable_path'

英文:

My code:

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. option = webdriver.ChromeOptions()
  4. driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=option)
  5. driver.get('https://www.google.com/')

Output:

  1. WebDriver.__init__() got an unexpected keyword argument 'executable_path'

I'm trying to create a script to log in to a website. When I try to run this script, it gives me this error:
WebDriver.__init__() got an unexpected keyword argument 'executable_path'

答案1

得分: 28

这是由于selenium版本4.10.0中的更改导致的:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

请注意,executable_path已被移除。

如果您想传递executable_path,现在必须使用service参数。

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. service = Service(executable_path='./chromedriver.exe')
  4. options = webdriver.ChromeOptions()
  5. driver = webdriver.Chrome(service=service, options=options)
  6. # ...
  7. driver.quit()
英文:

This is due to changes in selenium 4.10.0:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' in Selenium Python

Note that executable_path was removed.

If you want to pass in an executable_path, you'll have to use the service arg now.

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. service = Service(executable_path='./chromedriver.exe')
  4. options = webdriver.ChromeOptions()
  5. driver = webdriver.Chrome(service=service, options=options)
  6. # ...
  7. driver.quit()

答案2

得分: 9

从参数中删除executable_url,因为如果你安装的是Selenium的最新版本,且版本在4.6.0以上,就不需要添加executable_url。在最新版本的Selenium中,也不需要下载webdriver

只需复制下面的代码并运行你的Python文件:

  1. from selenium import webdriver
  2. driver = webdriver.Chrome()
  3. driver.get("https://www.facebook.com/")
英文:

Note: Remove executable_url from the argument, because you have installed the latest version of Selenium if you have selenium above the 4.6.0 you don't need to add executable_url and in the latest version of Selenium you don't need to download webdriver.

just copy the below code and run the your python file simple

  1. from selenium import webdriver
  2. driver=webdriver.Chrome()
  3. driver.get("https://www.facebook.com/")

答案3

得分: 2

只需删除executable_path(如下所示),如果您不希望手动设置driver.exe路径。使用最新的selenium(v4.6.0及更高版本),其内置工具称为SeleniumManger可以在不指定时下载和处理driver.exe

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. option = webdriver.ChromeOptions()
  4. driver = webdriver.Chrome(options=option)
  5. driver.get('https://www.google.com/')
英文:

Just remove executable_path(see below), if you do not wish to set the driver.exe path manually. With latest selenium(v4.6.0 and onwards), its in-built tool known as SeleniumManger can download and handle the driver.exe if you do not specify.

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. option = webdriver.ChromeOptions()
  4. driver = webdriver.Chrome(options = option)
  5. driver.get('https://www.google.com/')

答案4

得分: 1

最新版本的Selenium不需要WebDriver。您可以从参数中删除executable_url,因为您已安装了最新版本的Selenium。

英文:

the latest selenium doesnt require a webdriver. you can remove executable_url from the argument, because you have installed the latest version of Selenium

答案5

得分: 0

我在这个GitHub帖子中帮助解决了这个问题:https://github.com/clemfromspace/scrapy-selenium/issues/128。请注意,我正在使用Scrapy创建网络爬虫,并使用Selenium与网站进行交互。

  • 转到ton77v的提交5c3fe7b,并复制他在middlewares.py中的代码。
  • 将middlewares.py代码替换为您本地计算机上scrapy_selenium包中的middlewares.py代码(对我来说,它在C:/Users//AppData/Local/anaconda3/Lib/site-packages/scrapy_selenium/middlewares.py中)。
  • [可选]:我还必须!pip install webdriver-manager。
    对于您的Scrapy爬虫,您需要修改settings.py文件(这是在启动Scrapy项目时出现的配置文件之一,如items.py、middlewares.py、pipelines.py和settings.py)。将以下代码添加到settings.py文件中:

    • SELENIUM_DRIVER_NAME = 'chrome'
    • SELENIUM_DRIVER_EXECUTABLE_PATH = None #实际上不是必需的,即使您注释掉此行,它也会工作
    • SELENIUM_DRIVER_ARGUMENTS=[] #在括号中加入'--headless'以防止浏览器弹出
  • 然后在终端中输入scrapy runspider <scraper_name>.py,然后享受吧!

关于正在发生的事情的快速解释:

  • 您正在让Scrapy安装BrowserDriverManager,不需要再指定BrowserDriverManager的位置
  • 美妙的是,在第一次安装BrowserDriverManager后,它会记住安装位置,并在后续运行中使用已安装的BrowserDriverManager
  • 您可以通过修改middlewares.py文件(让ChatGPT为您执行)和更改SELENIUM_DRIVER_NAME =(浏览器名称)来使爬虫适应其他浏览器。
英文:

I helped solve this in this github post: https://github.com/clemfromspace/scrapy-selenium/issues/128. Please note I'm using scrapy to create web scrapers and Selenium to interact with the website.

  • go to ton77v's commit 5c3fe7b and copy his code in middlewares.py
  • replace the middlewares.py code under the scrapy_selenium package on your local machine (for me, it was in C:/Users//AppData/Local/anaconda3/Lib/site-packages/scrapy_selenium/middlewares.py)
  • [optional]: I had to !pip install webdriver-manager as well
    for your scrapy spider, you need to modify the settings.py file (this is part of the configuration files that appear when you start a scrapy project like items.py, middlewares.py, pipelines.py, and settings.py). Add the following lines of code into the settings.py file

    • SELENIUM_DRIVER_NAME = &#39;chrome&#39;
    • SELENIUM_DRIVER_EXECUTABLE_PATH = None #not actually necessary, will work even if you comment this line out
    • SELENIUM_DRIVER_ARGUMENTS=[] #put &#39;--headless&#39; in the brackets to prevent browser popup
  • then enter scrapy runspider &lt;scraper_name&gt;.py in your terminal and enjoy!

Quick explanation of what's happening:

  • you're getting scrapy to install the BrowserDriverManager and don't have to specify the BrowserDriverManager location anymore
  • the beauty is that after the first BrowserDriverManager installation, it remembers the installation location and uses the installed BrowserDriverManager for subsequent runs
  • You can adapt the scraper to open other browsers by modifying middlewares.py file (get ChatGPT to do it for you XD) and changing SELENIUM_DRIVER_NAME = (browser name)

答案6

得分: 0

你可以尝试这个方法

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service as ChromeService
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. options = webdriver.ChromeOptions()
  5. driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
英文:

You can try this method

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service as ChromeService
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. options = webdriver.ChromeOptions()
  5. driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)

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

发表评论

匿名网友

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

确定