selenium 連接已經(jīng)打開(kāi)的chrome瀏覽器 MAC
一,前言
今天在爬取chatGPT的谷歌插件的prompts的時(shí)候,發(fā)現(xiàn)繞不過(guò)他的反爬機(jī)制,失敗+1+1+1,所以想用連接已打開(kāi)的chatGPT頁(yè)面進(jìn)行控制
二,具體步驟
1,添加環(huán)境變量
用臨時(shí)在終端添加環(huán)境變量的方法,方便又快捷了屬實(shí)是,新打開(kāi)一個(gè)終端復(fù)制粘貼即可。
export PATH="/Applications/Google Chrome.app/Contents/MacOS:$PATH"
source ~/.bashrc
測(cè)試有沒(méi)有添加環(huán)境變量成功:
echo $PATH
當(dāng)看到多了 /Applications/Google Chrome.app/Contents/MacOS就代表臨時(shí)添加成功了。
2,啟動(dòng)Chrome調(diào)試模式
Google\ Chrome --remote-debugging-port=9222 --user-data-dir="~/ChromeProfile"
運(yùn)行這個(gè)后,就可以看見(jiàn)一個(gè)chrome打開(kāi)了,接下來(lái)寫(xiě)程序連接它:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-440370.html
3,模版程序
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'])
chromedriver_path='/Users/yanghao31/Desktop/software/chromedriver'#換成你的chromedriver的絕對(duì)地址
driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)
url = 'https://www.tmall.com/'
driver.get(url)
# 這兩個(gè)屬性可以用來(lái)做斷言使用
print("當(dāng)前頁(yè)面標(biāo)題:", driver.title)
print("當(dāng)前頁(yè)面的url:", driver.current_url)
運(yùn)行上面的代碼,會(huì)發(fā)現(xiàn)它連接到你剛才打開(kāi)的瀏覽器
并輸出天貓和https://www.tmall.com/。
注:當(dāng)get中的url,瀏覽器中沒(méi)有打開(kāi)的時(shí)候會(huì)自動(dòng)打開(kāi)該頁(yè)面,當(dāng)已打開(kāi)的時(shí)候,則會(huì)刷新不過(guò)不需要重新登陸。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-440370.html
到了這里,關(guān)于selenium 連接已經(jīng)打開(kāi)的chrome瀏覽器 MAC的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!