700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > mac selenium 连接已经打开的chrome浏览器

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

时间:2020-03-12 16:26:32

相关推荐

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 webdriverfrom mon.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver import ActionChainsoptions = 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='/'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/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。