[英]Python Selenium Chrome Webdriver
I'm beginning the automate the boring stuff book and I'm trying to open a chrome web browser through python. 我开始自动化无聊的东西书,并且尝试通过python打开chrome网络浏览器。 I have already installed selenium and
我已经安装了硒和
I have tried to run this file: 我试图运行此文件:
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')
But because of that I get this Error: 但是正因为如此,我得到这个错误:
Traceback (most recent call last): File "C:\Program Files
(x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.py",
line 74, in start
stdout=self.log_file, stderr=self.log_file) File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session) File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred: 在处理上述异常期间,发生了另一个异常:
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-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py",
line 62, in __init__
self.service.start() File "C:\Program Files (x86)\Python36-32\lib\site-packages\selenium\webdriver\common\service.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
You need to specify the path where your chromedriver is located . 您需要指定chromedriver所在的路径 。
Download chromedriver for your desired platform from here . 从此处下载适用于所需平台的chromedriver 。
Place chromedriver on your system path, or where your code is. 将chromedriver放置在系统路径或代码所在的位置。
If not using a system path, link your chromedriver.exe
(For non-Windows users, it's just called chromedriver
): 如果不使用系统路径,请链接您的
chromedriver.exe
(对于非Windows用户,仅称为chromedriver
):
browser = webdriver.Chrome(executable_path=r"C:\\path\\to\\chromedriver.exe")
(Set executable_path
to the location where your chromedriver is located.) (将
executable_path
路径设置为chromedriver所在的位置。)
If you've placed chromedriver on your System Path, you can shortcut by just doing the following: 如果您在系统路径上放置了chromedriver,则可以通过以下操作来实现快捷方式:
browser = webdriver.Chrome()
If you're running on a Unix-based operating system, you may need to update the permissions of chromedriver after downloading it in order to make it executable: 如果您在基于Unix的操作系统上运行,则可能需要在下载chromedriver后更新chromedriver的权限才能使其可执行:
chmod +x chromedriver
That's all. 就这样。 If you're still experiencing issues, more info can be found on this other StackOverflow article: Can't use chrome driver for Selenium
如果您仍然遇到问题,可以在此其他StackOverflow文章中找到更多信息: 不能对Selenium使用chrome驱动程序
Here's a simpler solution: install python-chromedrive package, import it in your script, and it's done. 这是一个更简单的解决方案:安装python-chromedrive软件包,将其导入您的脚本中,然后完成。
Step by step : 逐步 :
1. pip install chromedriver-binary 1. pip安装chromedriver-binary
2. import the package 2.导入包
from selenium import webdriver
import chromedriver_binary # Adds chromedriver binary to path
driver = webdriver.Chrome()
driver.get("http://www.python.org")
Reference: https://pypi.org/project/chromedriver-binary/ 参考: https : //pypi.org/project/chromedriver-binary/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.