使用 selenium 的 webdriver 时出错

问题描述 投票:0回答:1

我正在尝试使用硒进行网络抓取。我运行了这段代码:

from selenium import webdriver
PATH = "C:\\Users\\winwin\\Documents\\Visual Studio\\chromedriver.exe"
URL = "https://scratch.mit.edu/projects/994662672/"
driver = webdriver.Chrome(PATH)
driver.get(URL)

但是显示错误:

Exception has occurred: AttributeError
'str' object has no attribute 'capabilities'
AttributeError: 'str' object has no attribute 'capabilities'
During handling of the above exception, another exception occurred:
File "C:\Users\winwin\Documents\Visual Studio\Python\ScratchScraping\main.py", line 6, in <module>
driver = webdriver.Chrome(PATH)
         ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'capabilities'

我该怎么办?

我知道我正确设置了 selenium 和网络驱动程序。

python selenium-webdriver web-scraping webdriver
1个回答
0
投票

试试这个:

from selenium import webdriver

PATH = "C:\\Users\\winwin\\Documents\\Visual Studio\\chromedriver.exe"
URL = "https://scratch.mit.edu/projects/994662672/"
driver = webdriver.Chrome(executable_path=PATH)
driver.get(URL)

通过指定executable_path=PATH,您可以清楚地表明 PATH 变量包含 ChromeDriver 可执行文件的路径,这可能会解决您所看到的 AttributeError 问题。

© www.soinside.com 2019 - 2024. All rights reserved.