在Mac上将硒与chromedriver一起使用

wyx*_*wyx 8 python selenium

我想在Mac上将硒与chromedriver一起使用,但遇到了一些麻烦。

  1. 我从下载chromedriver ChromeDriver-WebDriver for Chrome
  2. 但我不想将其放入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")
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)
  1. 然后我运行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中。

那么如何解决呢?

解:

  1. brew cask install chromedriver
  2. which chromedriver 获取驱动程序路径
  3. 然后像这样使用 webdriver.Chrome("/usr/local/bin/chromedriver")

但是我不知道为什么使用硒如此复杂。

Has*_*man 7

为简单起见:

从此链接下载 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)


Sau*_*aur 6

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)

使用命令PATH变量设置为可执行文件

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)


小智 5

对我来说,像这样工作而不会使事情复杂化

  1. 从官方链接下载chromedriver(Chrome浏览器注意版本)
  2. 解压*.zip文件并将文件chromedriver复制到位置usr/local/bin/
  3. 删除您放在文件中的任何路径,然后使用driver = webdriver.Chrome()
  4. 如果probem仍然存在,请尝试重新打开PyCharm,因为有时需要重新打开才能工作