在Mac Chrome上使用Selenium

40

最近我买了一台Mac电脑,安装了Mozilla浏览器后没有遇到任何问题,但是安装Chrome扩展和在其中运行Selenium时遇到了麻烦。有人可以指导我在Mac Chrome上安装扩展并运行Selenium的过程吗?

8个回答

40
如果您想要使用 Selenium WebDriver 和 Chrome 浏览器,首先需要下载 ChromeDriver - 适用于 Chrome 的 WebDriver。可以通过 Homebrew 进行安装:brew install chromedriver,或者手动下载、提取、移动,并按照以下方式设置 PATH
$ cd $HOME/Downloads
$ wget http://chromedriver.storage.googleapis.com/2.22/chromedriver_mac32.zip
$ unzip chromedriver_mac32.zip
$ mkdir -p $HOME/bin
$ mv chromedriver $HOME/bin
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bash_profile

来源:在Mac或Linux上安装和设置Chromedriver路径

接下来,您应该阅读在桌面上使用ChromeDriver入门,特别是示例代码,其中显示了如何映射可执行文件的路径并实例化ChromeDriver。如果您在PATH变量中引用驱动程序,则可以省略配置行。

您可以通过ChromeDriver安装打包(.crx文件)和未打包(目录)扩展。有关设置任一扩展的代码段,请参见此处

如果您之前使用的是火狐浏览器的Selenium IDE,则无法使用Chrome浏览器的版本。我知道的最佳替代品是iMacros for Chrome


谢谢您的回答,您能否请指导我安装brew install chromedriver的过程。我尝试从终端安装它,但似乎无法完成。我也在网上搜索了解决方案,但没有成功。 - Ravi
@Ravi 你需要安装 Homebrew 包管理器。你只需要运行他们在网站上放置的命令,它会下载安装源并通过 Ruby 解释器执行。 - Gideon Pyzer
我已经在终端(MacOS)上使用“brew tap homebrew/cask”和“brew cask install chromedriver”完成了操作。 - Nicolaesse

28

有时候你会在使用旧版本的chromedriver时遇到问题,而安装它时使用以下命令:

brew cask install chromedriver

它向您展示以下内容:

Error: It seems there is already a Binary at '/usr/local/bin/chromedriver'; not linking.

然而,您可以采取以下步骤:

brew cask reinstall chromedriver
如果仍然显示相同的错误,您可以使用以下命令将其删除。
rm /usr/local/bin/chromedriver

并重新安装它

brew cask install chromedriver

您应该拥有更新的 Chrome 驱动程序版本。


1
谢谢伙计。我没想到只需要删除驱动程序并重新安装即可。 - Ashish

19

我认为在Mac OS终端上同时运行Mac OSX、Chrome和Selenium的简单方法如下:

# download selenium jar
curl -L0 https://selenium-release.storage.googleapis.com/3.9/selenium-server-standalone-3.9.1.jar -o selenium-server-standalone.jar

# install chromedriver using cask
brew cask install chromedriver

# start chrome driver
brew services start chromedriver                                                                                                                                                                      
#==> Successfully started `chromedriver` (label:homebrew.mxcl.chromedriver)

# start selenium server
java -jar selenium-server-standalone.jar                                                                                                                                                                           
#14:38:20.684 INFO - Selenium build info: version: '3.9.1', revision: '63f7b50'
#14:38:20.685 INFO - Launching a standalone Selenium Server on port 4444

4
当我运行 'brew services start chromedriver' 命令时,出现 'No available formula with the name "chromedriver"' 的错误信息。你有任何想法是为什么吗? - MLEN
也许现在已经有了一个新的名称或公式,请使用“brew services list”进行检查,它应该已经安装好了。 - anquegi
1
不行,即使运行了 brew cask install chromedriver,在 brew services list 下仍然没有任何内容。 - zasman

8

我想你有几个选择。

  • 手动下载Chrome驱动并将其添加到您的 PATH 中,
  • 或使用 webdriver_manager

手动下载chromedriver

第一种选择是手动下载chromedriver并将其添加到 PATH 中:

步骤1:下载 chromedriver

# You can find more recent/old versions at http://chromedriver.storage.googleapis.com/
wget http://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_mac64.zip

步骤二:将chromedriver添加到你的PATH中(例如:/usr/local/bin)。
unzip chromedriver_mac64.zip
cp chromedriver /usr/local/bin

现在,您应该能够运行

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://localhost:8000')

没有任何问题


使用webdriver_manager

或者,您可以使用包含大多数网络驱动程序的webdriver_manager

首先使用pip安装它:

pip install webdriver_manager

尤其是针对Chrome浏览器,您应该执行以下操作

from webdriver_manager.chrome import ChromeDriverManager


browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get('http://localhost:8000')

6
  1. One way is if you have homebrew on your mac, then on terminal, use this command brew install chromedriver
  2. Then you need to download chromedriver on your machine, do it from http://chromedriver.storage.googleapis.com/index.html Download latest version It will look like, "chromedriver_mac32.zip" (doesn't matter if its 32 bit, it will work for 64 bit MAC as well)
  3. Use this code for open Chrome if your chromedriver that you downloaded is inside your project folder and looks like this ..Project folder/Chrome/chromedriver

     System.setProperty("webdriver.chrome.driver", 
     System.getProperty("user.dir")+"/Chrome/chromedriver");
     driver=new ChromeDriver();
    

感谢您的提示,只需列出chromedriver的实际完整路径! - MethodMan

3

使用 Chrome 驱动器,从这里下载:https://sites.google.com/chromium.org/driver/

将文件添加到 PyCharm 项目中,然后使用此 cmd 命令禁用权限弹窗。

xattr -d com.apple.quarantine chromedriver

在 Python 程序中,使用以下代码行,请确保 Chrome 已安装在应用程序中。
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'

driver = webdriver.Chrome('/Users/<Username>/PycharmProjects/selenium_py/chromedriver')
driver.get('https://www.google.com')

2

使用brew install chromedriver直接安装chromedriver。chrome驱动程序位于/usr/local/bin/chromedriver。 如果您重新加载终端,可以使用source ~/.bashrcsource ~/.zshrc(根据您的设置),或者重新启动终端。 如果仍然无法正常工作,请将chrome驱动程序添加到PATH中。您需要通过添加以下行将/usr/local/bin/chromedriver添加到您的~/.bashrc或~/.zshrc: export PATH=$HOME/bin:/usr/local/bin:$PATH


0
在下载正确的chromedriver之后,最简单的方法是使用一行命令: sudo chmod a+x chromedriver && sudo mv chromedriver /usr/local/bin/chromedriver


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接