我想在Mac上使用chlenium和chromedriver,但我有一些麻烦。
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
。我只运行它
没有司机路径。
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
但它也无法发挥作用。
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
。但是
selenium.common.exceptions.WebDriverException:消息:&#39; chromedriver&#39;可执行文件需要在PATH中。
所以如何解决它,我想将它与不在PATH中的驱动程序路径一起使用,这样我就可以轻松部署我的项目了。
解决方案:
brew cask install chromedriver
which chromedriver
获取驱动程序路径webdriver.Chrome("/usr/local/bin/chromedriver")
但我不知道为什么使用硒这么复杂。
答案 0 :(得分:4)
selenium.common.exceptions.WebDriverException:消息:&#39; chromedriver&#39;可执行文件需要在PATH中。
To launch chrome browser using ChromeDriver
您需要将executable chromedriver位置与可执行文件本身一起传递到executable_path
。
你应该尝试如下: -
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/')
答案 1 :(得分:2)
对我来说,这样工作不会使事情复杂化
答案 2 :(得分:1)
为简单起见:
从此link下载chrome webdriver。 复制&#39; chromedriver&#39;在python脚本的文件夹中。
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()
答案 3 :(得分:0)
对我来说,通过自制软件安装chromedriver就像在MacOS 11上一样具有魅力。
brew install chromedriver && brew update