mac selenium 连接已经打开的chrome浏览器

今天在mac环境下尝试了一下用selenium连接现有的服务器,本来想绕过某宝的反爬虫机制的,但是并没有什么用,但是这个技术不错,我这里分享一下实现过程。

  • 添加环境变量
export PATH="/Applications/Google Chrome.app/Contents/MacOS:$PATH"

把上面的这一句添加到bashrc中,我的是zshrc,然后激活环境:

source ~/.zshrc

然后打开chrome:

Google\ Chrome --remote-debugging-port=9222 --user-data-dir="~/ChromeProfile"

运行这个后,就可以看见一个chrome打开了,接下来写程序连接它:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

options = webdriver.ChromeOptions()

options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
# options.add_experimental_option('excludeSwitches', ['enable-automation'])
browser = webdriver.Chrome(executable_path=chromedriver_path, options=options)

url='https://www.tmall.com/'

browser.get(url)

运行上面的代码,会发现它连接到的是你刚才打开的浏览器,是不是很简单。

参考文献

[1].How to connect Selenium to an existing browser that was opened manually?. https://cosmocode.io/how-to-connect-selenium-to-an-existing-browser-that-was-opened-manually/

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值