可以使用selenium的webdriver.Remote()方法连接已经打开的浏览器,具体步骤如下:

  1. 在已经打开的浏览器中,打开一个空白页面,地址为http://localhost:9515。

  2. 在Python中使用selenium的webdriver.Remote()方法连接已经打开的浏览器,代码如下:

from selenium import webdriver

# 连接已经打开的浏览器
driver = webdriver.Remote(command_executor='http://localhost:9515', desired_capabilities={})
  1. 连接成功后,就可以使用driver对象进行操作了,例如获取当前页面的标题:
title = driver.title
print(title)

注意:连接已经打开的浏览器时,需要确保已经打开的浏览器与selenium使用的浏览器驱动版本一致,否则可能会出现兼容性问题。

python用selenium和已经打开浏览器进行连接不要创建新的浏览器对象

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

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