我想在Mac上使用selenium和chromedriver,但我遇到了一些麻烦。
我从ChromeDriver - WebDriver for Chrome
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/')
但是我不能得到我想要的结果。
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
brew cask install chromedriver
.And,我只在没有驱动路径的情况下运行。browser.get('http://www.baidu.com/') = webdriver.Chrome() browser
但它也不能工作。
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
最后,我尝试将它放到/usr/bin中
➜ Downloads sudo cp chromedriver /usr/bin
Password:
cp: /usr/bin/chromedriver: Operation not permitted
我尝试在.zshrc中使用export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
。但
chromedriver消息:‘
selenium.common.exceptions.WebDriverException:’可执行文件需要在PATH中。
所以如何解决它,我想使用它的驱动路径,而不是在路径中,这样我就可以轻松地部署我的项目。
解决方案:
brew cask install chromedriver
which chromedriver
获取但是我不知道为什么使用selenium这么复杂。
发布于 2016-09-11 00:48:35
chromedriver消息:‘
selenium.common.exceptions.WebDriverException:’可执行文件需要在PATH中。
To launch chrome browser using ChromeDriver
你需要将executable chromedriver位置连同可执行文件本身一起传递到executable_path
中。
你应该尝试如下所示:
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")
browser = webdriver.Chrome(executable_path = DRIVER_BIN)
browser.get('http://www.baidu.com/')
或将PATH
变量using command with executable设置为:-
export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
然后尝试将ChromeDriver
初始化为:-
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
发布于 2018-06-05 09:11:39
为了简单起见:
从这个link下载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()
发布于 2019-08-17 05:58:43
因为我是这样工作的,不会让事情变得复杂
https://stackoverflow.com/questions/39428042
复制相似问题