使用Selenium控制已經打開的瀏覽器


有時候網站需要掃碼登錄,使用Selenium啟動的瀏覽器進程可能沒法完成登錄。這時候就需要手動掃碼登錄后,再用Selenium進行操作。

 

操作步驟

1、找到本地安裝的瀏覽器啟動路徑,例如Chrome

C:\Program Files (x86)\Google\Chrome\Application

2、通過命令行啟動ChromeDbug模式

C:\Program Files (x86)\Google\Chrome\Application>chrome.exe --remote-debugging-port=9222

快捷切換到該目錄下(按住shift+鼠標右鍵)

3、連接調試開關打開的chrome

options = webdriver.ChromeOptions()
options.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=options)

 

企業版微信登錄

先執行命令啟動ChromeDbug模式

掃碼登錄企業微信

使用Selenium點擊通訊錄進入通訊錄頁面

 1 # -*- coding: utf-8 -*-
 2 from selenium import webdriver
 3 from selenium.webdriver.common.by import By
 4 class TestWX:
 5 # 初始化,控制Chrome
 6     def setup_method(self):
 7         options = webdriver.ChromeOptions()
 8         options.debugger_address = "127.0.0.1:9222"
 9         self.driver = webdriver.Chrome(options=options)
10         self.url = "https://work.weixin.qq.com/wework_admin/frame#index"
11         self.driver.get(self.url)
12         self.driver.implicitly_wait(10)
13 # 關閉瀏覽器
14 #     def teardown_method(self):
15 #         self.driver.quit()
16     def test_a(self):
17 # 點擊通訊錄
18         self.driver.find_element(By.XPATH, '//*[@id="menu_contacts"]/span').click()
19         
20 a=TestWX()
21 a.setup_method()
22 a.test_a()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM