我想在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/')
Run Code Online (Sandbox Code Playgroud)
但是我无法获得想要的结果。
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
Run Code Online (Sandbox Code Playgroud)
然后我运行brew cask install chromedriver
。并且我只在没有驱动程序路径的情况下运行它。
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
Run Code Online (Sandbox Code Playgroud)但这也不行。
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
Run Code Online (Sandbox Code Playgroud)
最后,我尝试将其放入/ usr / bin
? Downloads sudo cp chromedriver /usr/bin
Password:
cp: /usr/bin/chromedriver: Operation not permitted
Run Code Online (Sandbox Code Playgroud)
我尝试使用 export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
在.zshrc中。但
selenium.common.exceptions.WebDriverException:消息:“ chromedriver”可执行文件必须位于PATH中。
那么如何解决呢?
解:
brew cask install chromedriver
which chromedriver
获取驱动程序路径webdriver.Chrome("/usr/local/bin/chromedriver")
但是我不知道为什么使用硒如此复杂。
为简单起见:
从此链接下载 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()
Run Code Online (Sandbox Code Playgroud)
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/')
Run Code Online (Sandbox Code Playgroud)
export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
Run Code Online (Sandbox Code Playgroud)
然后尝试初始化ChromeDriver
为:-
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
Run Code Online (Sandbox Code Playgroud)