接口自动化测试-用Selenium控制已经打开的浏览器
更新时间:2021-09-27 01:58:32 作者:admin 浏览:497
接口自动化测试-用Selenium控制已经打开的浏览器
有时候网站需要扫码登录,使用selenium启动的浏览器进程可能没法完成登录。这时就需要先手动启动浏览器扫码登录后,再用selenium进行操作。
操作步骤
1.找到本地安装的浏览器启动路径,例如Chrome
# windows
C:Program Files (x86)GoogleChromeApplicationchrome.exe
# mac
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
2.通过命令行启动ChromeDbug模式
# windows
$ C:Program Files (x86)GoogleChromeApplication>chrome.exe --remote-debugging-port=9222
# mac
$ /Applications/Google Chrome.app/Contents/MacOS/Google Chrome -remote-debugging-port=9222
# 注:
1. 启动浏览器dbug模式时需要把浏览器打开的进程先全部关闭。
2. 9222是默认端口,可以随意修改。但别使用已经被占用的端口。
更多chromeDbug操作参考 chromedevtools
3.连接调试开关打开的chrome
options = webdriver.ChromeOptions()
options.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=options)
实例企业微信
1.先执行命令启动浏览器DBUG模式
$ C:Program Files (x86)GoogleChromeApplication>chrome.exe --remote-debugging-port=9222
2.扫码登录企业微信
企业微信首页
3.使用selenium点击通讯录进入通讯录页面
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestWX:
# 初始化,控制Chrome
def setup_method(self):
options = webdriver.ChromeOptions()
options.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=options)
self.url = "https://work.weixin.qq.com/wework_admin/frame#index"
self.driver.get(self.url)
self.driver.implicitly_wait(10)
# # 关闭浏览器
# def teardown_method(self):
# self.driver.quit()
def test_a(self):
# 点击通讯录
self.driver.find_element(By.XPATH, '//*[@id="menu_contacts"]/span').click()
执行结果
企业微信通讯录页面
ok,以上就完成了通过selenium控制已经打开的页面。
总结
启动一个Chrome的debug进程
通过webdriver连接到Chrome的9222端口
selenium通过webdriver控制Chrome
以上是关于用Selenium控制已经打开的浏览器的介绍,由多测师亲自撰写。https://www.aichudan.com/