Python Selenium Chrome Webdriver [重复]
- 2025-03-07 09:03:00
- admin 原创
- 46
问题描述:
我正在开始自动化无聊的东西这本书,我正在尝试通过python打开chrome网络浏览器。我已经安装了selenium和
我曾尝试运行此文件:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('https://automatetheboringstuff.com')
但正因为如此,我得到了这个错误:
Traceback (most recent call last): File "C:Program Files
(x86)Python36-32libsite-packagesseleniumwebdrivercommonservice.py",
line 74, in start
stdout=self.log_file, stderr=self.log_file) File "C:Program Files (x86)Python36-32libsubprocess.py", line 707, in __init__
restore_signals, start_new_session) File "C:Program Files (x86)Python36-32libsubprocess.py", line 990, in _execute_child
startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified
在处理上述异常的过程中,又发生了另一个异常:
Traceback (most recent call last): File "C:/Program Files
(x86)/Python36-32/test.py", line 5, in <module>
browser = webdriver.Chrome() File "C:Program Files (x86)Python36-32libsite-packagesseleniumwebdriverchromewebdriver.py",
line 62, in __init__
self.service.start() File "C:Program Files (x86)Python36-32libsite-packagesseleniumwebdrivercommonservice.py",
line 81, in start
os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
executable needs to be in PATH. Please see
https://sites.google.com/a/chromium.org/chromedriver/home
解决方案 1:
您需要指定您的 chromedriver 所在的路径。
从这里下载适合您所需平台的 chromedriver。
将 chromedriver 放在您的系统路径上,或者您的代码所在的位置。
如果不使用系统路径,请链接您的
chromedriver.exe
(对于非 Windows 用户,它仅称为chromedriver
):
browser = webdriver.Chrome(executable_path=r"C:path ochromedriver.exe")
(设置executable_path
为您的 chromedriver 所在的位置。)
如果您已将 chromedriver 放在系统路径上,则只需执行以下操作即可快捷操作:
browser = webdriver.Chrome()
如果您在基于 Unix 的操作系统上运行,则可能需要在下载 chromedriver 后更新其权限以使其可执行:
chmod +x chromedriver
就这样。如果您仍然遇到问题,可以在另一篇 StackOverflow 文章中找到更多信息:无法将 chrome 驱动程序用于 Selenium
解决方案 2:
这是一个更简单的解决方案:安装 python-chromedrive 包,将其导入到脚本中,就完成了。
步骤如下:
pip install chromedriver-binary
导入包
from selenium import webdriver
import chromedriver_binary # Adds chromedriver binary to path
driver = webdriver.Chrome()
driver.get("http://www.python.org")
参考:https ://pypi.org/project/chromedriver-binary/
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD