Python 在 Mac 上通过 chromedriver 使用 selenium
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39428042/
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
Use selenium with chromedriver on Mac
提问by wyx
I want to use selenium with chromedriver on Mac,but I have some troubles on it.
我想在 Mac 上将 selenium 与 chromedriver 一起使用,但是我遇到了一些麻烦。
- I download the chromedriver from ChromeDriver - WebDriver for Chrome
- But I don't want to put it to PATH.So I do this.
- 我从ChromeDriver - WebDriver for Chrome下载了 chromedriver
- 但我不想把它放到 PATH.So 我这样做。
import os
from selenium import webdriver
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "bin/chromedriver_for_mac")
print DRIVER_BIN
browser = webdriver.Chrome(DRIVER_BIN)
browser.get('http://www.baidu.com/')
But I can't get the result I want.
但我无法得到我想要的结果。
Traceback (most recent call last):
File "/Users/wyx/project/python-scraping/se/test.py", line 15, in <module>
browser = webdriver.Chrome(DRIVER_BIN)
File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
self.service.start()
File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver_for_mac' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x107f96150>> ignored
Then I run
brew cask install chromedriver
.And I only run this without the driver path.browser = webdriver.Chrome() browser.get('http://www.baidu.com/')
然后我运行
brew cask install chromedriver
。我只在没有驱动程序路径的情况下运行它。browser = webdriver.Chrome() browser.get('http://www.baidu.com/')
But it can't work either.
但它也不能工作。
Traceback (most recent call last):
File "/Users/wyx/project/python-scraping/se/test.py", line 16, in <module>
browser = webdriver.Chrome()
File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
self.service.start()
File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, 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
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x105c08150>> ignored
Process finished with exit code 1
Finally I try to put it to /usr/bin
最后我试着把它放到 /usr/bin
? Downloads sudo cp chromedriver /usr/bin
Password:
cp: /usr/bin/chromedriver: Operation not permitted
and I try to use export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
in .zshrc . But
我尝试export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
在 .zshrc 中使用。但
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.
selenium.common.exceptions.WebDriverException: 消息:'chromedriver' 可执行文件需要在 PATH 中。
So how to solve it,I want to use it with the driver path not in the PATH so I can deploy my project easily.
那么如何解决呢,我想使用它的驱动程序路径不在PATH中,这样我就可以轻松部署我的项目。
Solution:
解决方案:
brew cask install chromedriver
which chromedriver
get the driver path- And then use it like this
webdriver.Chrome("/usr/local/bin/chromedriver")
brew cask install chromedriver
which chromedriver
获取驱动程序路径- 然后像这样使用它
webdriver.Chrome("/usr/local/bin/chromedriver")
But I don't know why so complex to use selenium.
但我不知道为什么使用硒这么复杂。
采纳答案by Saurabh Gaur
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.
selenium.common.exceptions.WebDriverException: 消息:'chromedriver' 可执行文件需要在 PATH 中。
To launch chrome browser using ChromeDriver
you need to pass executable chromedriverlocation with executable itself into executable_path
.
要使用 Chrome 浏览器启动,ChromeDriver
您需要将可执行文件本身的可执行 chromedriver位置传递到executable_path
.
You should try as below :-
你应该尝试如下: -
from selenium import webdriver
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "bin/chromedriver_for_mac")
browser = webdriver.Chrome(executable_path = DRIVER_BIN)
browser.get('http://www.baidu.com/')
Or set PATH
variable using command with executableas :-
或使用可执行文件的命令设置PATH
变量为:-
export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
Then try to Initialize ChromeDriver
as :-
然后尝试初始化ChromeDriver
为:-
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
回答by Hassan Rahman
For sake of simplicity:
为简单起见:
Download the chrome webdriver from this link. Copy the 'chromedriver' in the folder of python script.
从此链接下载 chrome webdriver 。复制python脚本文件夹中的'chromedriver'。
from selenium import webdriver
import os
url = 'http://www.webscrapingfordatascience.com/complexjavascript/'
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")
driver = webdriver.Chrome(executable_path = DRIVER_BIN)
driver.get(url)
input('Press ENTER to close the automated browser')
driver.quit()
回答by Milos
For me worked like this without complicating things
对我来说,像这样工作而不会使事情复杂化
- Download chromedriverfrom official link(notice version of Chrome browser)
- Unpack *.zipfile and file chromedrivercopy to location usr/local/bin/
- Remove any path you put in file and just go with driver = webdriver.Chrome()
- If probem still exist try to reopen PyCharm since sometimes need to be reopened in case to work
- 从官方链接下载chromedriver(Chrome浏览器注意版本)
- 解压*.zip文件并将文件chromedriver复制到位置usr/local/bin/
- 删除您放在文件中的任何路径,然后使用driver = webdriver.Chrome()
- 如果probem仍然存在,请尝试重新打开PyCharm,因为有时需要重新打开才能工作