songzhenhua
Selenium编写用例的时候,需要不断的运行调试debug,但正常情况会每次重新打开一个Chrome运行,如果你的系统需要登陆,或者你正在调试的用例需要很长的前置步骤,都重新来一遍非常浪费时间。有没有办法可以让Selenium直接在已打开的浏览器上继续运行呢?有的:
 
首先使用命令打开Chrome浏览器并开启远程调试功能,打开CMD,输入
chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\selenium_ui_auto\chrome_temp"
 
 
如图,浏览器已打开了,我们输入百度地址打开百度首页,然后运行以下代码:
 
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = r"D:\code\python\selenium_ui_auto\driver\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
 
driver.find_element_by_id(\'kw\').send_keys(u\'测试工程师小站\')

 

可以看到,已打开的浏览器,原来在搜狗新闻页,但调试的用例假如是百度页,我手动输入百度后,运行代码,被输入了关键字\'测试工程师小站\',并显示了搜索结果。这样,以后调试的时候,就可以手动运行前置步骤,再运行代码了。
 

---------------------------------------------------------------------------------

关注微信公众号(测试工程师小站)即可在手机上查阅,并可接收更多测试分享,发送【测试资料】更可获取百G测试教程~

分类:

技术点:

相关文章:

  • 2021-04-19
  • 2022-01-29
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
  • 2021-08-16
猜你喜欢
  • 2021-10-10
  • 2021-08-13
  • 2021-09-15
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
相关资源
相似解决方案