在 Mac M1 上安装 selenium

自从主力机换成 M1 后,就很少开docker了,很多容器都没有做arm架构的镜像。我常用的 selenium 也是,可叹。最后还是裸着安装了。以前在这篇文章里折腾过本地安装《selenium 裸安装备忘》,这次也是类似的操作,在本篇文章里记录一下。

一、安装java

brew install java

image-20221117午後13156478

image-20221117午後13232702

export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"
alias java="/opt/homebrew/opt/openjdk/bin/java

image-20221117午後32601808

我发现 Mac 下的PATH有问题,在 zshrc 里改的不符合想法,系统的PATH还是在前面,我担心这样会有问题,遂直接改了这个文件 /etc/paths

二、安装chromedriver

先去官网下载 Chrome 版本对应的Chromedriver:https://chromedriver.chromium.org/downloads,扔到 path 对应的目录里。这里我扔到 ~/bin 目录下

image-20221117午後70447004

Mac 下允许执行权限,设置->安全 里:

Screenshot 8

三、安装selenium

官方地址:https://www.selenium.dev/downloads/

我下载了目前最新的 4.6.0版本:

image-20221125午後45446075

然后运行:

java -jar bin/selenium-server-4.6.0.jar standalone --host 100.100.100.9 --port 14444

IP 端口根据各自的需要配置了。

image-20221117午後72428922

看到日志显示 找到driver 就说明已经运行成功了。

尝试调用,看到输出了:

image-20221125午後45927768

16:57:45.003 INFO [LocalDistributor.newSession] - Session request received by the Distributor:
 [Capabilities {browserName: chrome, chromeOptions: {args: [--no-sandbox, window-size=1080x1920, --disable-gpu, --hide-scrollbars, --disable-extensions, start-maximized, --lang=zh-cn, --accept-lang=zh-cn, --arc-disable-locale-sync, --override-language-detection, --disable-dev-shm-usage]}, platform: ANY}, Capabilities {browserName: chrome, goog:chromeOptions: {args: [--no-sandbox, window-size=1080x1920, --disable-gpu, --hide-scrollbars, --disable-extensions, start-maximized, --lang=zh-cn, --accept-lang=zh-cn, --arc-disable-locale-sync, --override-language-detection, --disable-dev-shm-usage]}}]
Starting ChromeDriver 107.0.5304.62 (1eec40d3a5764881c92085aaee66d25075c159aa-refs/branch-heads/5304@{#942}) on port 43558
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

16:57:46.590 WARN [ProtocolHandshake.createSession] - Support for Legacy Capabilities is deprecated; You are sending the following invalid capabilities: [chromeOptions, platform]; Please update to W3C Syntax: https://www.selenium.dev/blog/2022/legacy-protocol-support/

16:57:46.606 INFO [LocalNode.newSession] - Session created by the Node. Id: d0aff398d25ed2e6c16003eb81336a8b, Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 107.0.5304.110, chrome: {chromedriverVersion: 107.0.5304.62 (1eec40d3a576..., userDataDir: /var/folders/k1/qfq95df561b...}, goog:chromeOptions: {debuggerAddress: localhost:49491}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: ANY, proxy: Proxy(), se:cdp: http://localhost:49491, se:cdpVersion: 107.0.5304.110, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}

16:57:46.611 INFO [LocalDistributor.newSession] - Session created by the Distributor. Id: d0aff398d25ed2e6c16003eb81336a8b
 Caps: Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 107.0.5304.110, chrome: {chromedriverVersion: 107.0.5304.62 (1eec40d3a576..., userDataDir: /var/folders/k1/qfq95df561b...}, chromeOptions: {args: [--no-sandbox, window-size=1080x1920, --disable-gpu, --hide-scrollbars, --disable-extensions, start-maximized, --lang=zh-cn, --accept-lang=zh-cn, --arc-disable-locale-sync, --override-language-detection, --disable-dev-shm-usage]}, goog:chromeOptions: {debuggerAddress: localhost:49491}, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: ANY, platformName: ANY, proxy: Proxy(), se:bidiEnabled: false, se:cdp: ws://100.100.100.9:14444/se..., se:cdpVersion: 107.0.5304.110, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}

四、番外-容器化

这两个命令是可以跑起来了:

docker run -d -p 4444:4444 --name selenium-hub selenium/hub:latest 
docker run -d --link selenium-hub:hub -e SE_EVENT_BUS_HOST=hub -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -e SE_EVENT_BUS_PUBLISH_PORT=4442 -p 5900:5900 --name selenium-node-chrome-vnc selenium/node-chrome:latest 

因为 arm下还没有相关的镜像,虽然容器可以运行

image-20231231142107145

image-20231231142135144

但是无法拉起chrome,用代码拉起chrome一定报错

image-20231231142327309

其实如果直接在vnc里启动chrome,也可以注意到,chrome是无法启动的:

image-20231231142419187

正常情况下应该是这个样子:

image-20231231184340328

所以如果有需要远程运行的话, 找一台x86的机器运行即可。我也是没想到,我x86的MacBook Air还能发挥余热。

docker run -d \
  --name selenium-chrome-1 \
  --restart always \
  --network bridge \
  -p 5901:5900 \
  -p 4445:4444 \
  -v /dev/shm:/dev/shm \
  selenium/standalone-chrome-debug

docker run -d \
  --name selenium-chrome-2 \
  --restart always \
  --network bridge \
  -p 5902:5900 \
  -p 4446:4444 \
  -v /dev/shm:/dev/shm \
  selenium/standalone-chrome-debug

docker run -d \
  --name selenium-chrome-3 \
  --restart always \
  --network bridge \
  -p 5903:5900 \
  -p 4447:4444 \
  -v /dev/shm:/dev/shm \
  selenium/standalone-chrome-debug

参考资料


pandas 操作 excel 备忘 laravel nova 不显示资源