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/

猜你喜欢

转载自blog.csdn.net/w5688414/article/details/106032555
今日推荐