Python Selenium Chrome Webdriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42478591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Selenium Chrome Webdriver
提问by Fernando Garibaldi
I'm beginning the automate the boring stuff book and I'm trying to open a chrome web browser through python. I have already installed selenium and
我正在开始自动化无聊的东西,我正在尝试通过 python 打开一个 chrome 网络浏览器。我已经安装了硒和
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
回答by Ahmad Taha
You need to specify the path where your chromedriver is located.
您需要指定 chromedriver 所在的路径。
Place chromedriver on your system path, or where your code is.
If not using a system path, link your
chromedriver.exe
(For non-Windows users, it's just calledchromedriver
):browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
(Set
executable_path
to the location where your chromedriver is located.)If you've placed chromedriver on your System Path, you can shortcut by just doing the following:
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:
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
将 chromedriver 放在您的系统路径或代码所在的位置。
如果不使用系统路径,请链接您的
chromedriver.exe
(对于非 Windows 用户,它只是称为chromedriver
):browser = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")
(设置
executable_path
为您的 chromedriver 所在的位置。)如果您已将 chromedriver 放在系统路径上,则只需执行以下操作即可快捷方式:
browser = webdriver.Chrome()
如果您在基于 Unix 的操作系统上运行,您可能需要在下载后更新 chromedriver 的权限以使其可执行:
chmod +x chromedriver
就这样。如果您仍然遇到问题,可以在另一篇 StackOverflow 文章中找到更多信息:Can't use chrome driver for Selenium
回答by Louis
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
2. import the package
一步一步:
1. pip install chromedriver-binary
2. 导入包
from selenium import webdriver
import chromedriver_binary # Adds chromedriver binary to path
driver = webdriver.Chrome()
driver.get("http://www.python.org")