我想在Mac上将硒与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
我尝试export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
在.zshrc中使用。但
selenium.common.exceptions.WebDriverException:消息:“ chromedriver”可执行文件必须位于PATH中。
因此,如何解决该问题,我想将其与驱动程序路径(而不是PATH)一起使用,以便可以轻松地部署我的项目。
解:
brew cask install chromedriver
which chromedriver
获取驱动程序路径webdriver.Chrome("/usr/local/bin/chromedriver")
但是我不知道为什么使用硒如此复杂。
selenium.common.exceptions.WebDriverException:消息:“ chromedriver”可执行文件必须位于PATH中。
要使用启动Chrome浏览器,ChromeDriver
您需要将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/')
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/')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句