mac selenium 连接已经打开的chrome浏览器
生活随笔
收集整理的這篇文章主要介紹了
mac selenium 连接已经打开的chrome浏览器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天在mac環境下嘗試了一下用selenium連接現有的服務器,本來想繞過某寶的反爬蟲機制的,但是并沒有什么用,但是這個技術不錯,我這里分享一下實現過程。
- 添加環境變量
把上面的這一句添加到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 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='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/
總結
以上是生活随笔為你收集整理的mac selenium 连接已经打开的chrome浏览器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大学里“最后悔”排行榜!你有没有中?
- 下一篇: 2017年html5行业报告,云适配发布