Python Selenium 自动化测试:使用调试端口控制浏览器
import os import time from selenium import webdriver from selenium.webdriver.chrome.options import Options
def check_browser():
#检查是否已经打开了浏览器
try:
options = Options()
options.add_experimental_option('debuggerAddress', '127.0.0.1:9527')
browser = webdriver.Chrome(options=options)
browser.execute_script('return true')
return True
except:
return False
#使用cmd命令打开一个浏览器 if name == 'main': if not check_browser(): os.system(r'start chrome --remote-debugging-port=9527 --user-data-dir='D:\评阅用'') #获取刚才打开的浏览器 options = Options() options.add_experimental_option('debuggerAddress', '127.0.0.1:9527') browser = webdriver.Chrome(options=options) browser.get('https://menhu.pt.ouchn.cn/site/ouchnPc/index') print(browser.title) input('请按任意键继续...') #添加等待用户收到运行后续功能的功能
原文地址: https://www.cveoy.top/t/topic/fYqw 著作权归作者所有。请勿转载和采集!