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': while not check_browser(): print('正在查找浏览器...') time.sleep(1) print('浏览器已找到!')

#获取刚才打开的浏览器
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)
Python Selenium 自动化测试:使用 Chrome 调试端口连接浏览器

原文地址: https://www.cveoy.top/t/topic/fYqu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录