使用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()

 

posted on 2019-08-23 16:01  Null_Bug  阅读(5357)  评论(1编辑  收藏  举报

导航