英文:
TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path' in Selenium Python
问题
来自 selenium 的 import webdriver
来自 selenium.webdriver.chrome.options 的 import Options
option = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=option)
driver.get('https://www.google.com/')
输出:
WebDriver.__init__() 收到了一个意外的关键字参数 'executable_path'
我试图创建一个用于登录网站的脚本。当我尝试运行此脚本时,它给我返回了这个错误:
WebDriver.__init__() 收到了一个意外的关键字参数 'executable_path'
英文:
My code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
option = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path='./chromedriver.exe', options=option)
driver.get('https://www.google.com/')
Output:
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
参数。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(executable_path='./chromedriver.exe')
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
英文:
This is due to changes in selenium
4.10.0
:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e
Note that executable_path
was removed.
If you want to pass in an executable_path
, you'll have to use the service
arg now.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(executable_path='./chromedriver.exe')
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
答案2
得分: 9
从参数中删除executable_url
,因为如果你安装的是Selenium的最新版本,且版本在4.6.0
以上,就不需要添加executable_url
。在最新版本的Selenium中,也不需要下载webdriver。
只需复制下面的代码并运行你的Python文件:
from selenium import webdriver
driver = webdriver.Chrome()
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
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://www.facebook.com/")
答案3
得分: 2
只需删除executable_path
(如下所示),如果您不希望手动设置driver.exe
路径。使用最新的selenium(v4.6.0
及更高版本),其内置工具称为SeleniumManger
可以在不指定时下载和处理driver.exe
。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
option = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=option)
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.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
option = webdriver.ChromeOptions()
driver = webdriver.Chrome(options = option)
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 fileSELENIUM_DRIVER_NAME = 'chrome'
SELENIUM_DRIVER_EXECUTABLE_PATH = None #not actually necessary, will work even if you comment this line out
SELENIUM_DRIVER_ARGUMENTS=[] #put '--headless' in the brackets to prevent browser popup
- then enter
scrapy runspider <scraper_name>.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
你可以尝试这个方法
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
英文:
You can try this method
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论