Python Selenium 连接已打开的谷歌浏览器
可以使用 Selenium 的 Remote WebDriver 来连接已经打开的浏览器。具体步骤如下:
-
在已经打开的浏览器中,输入
chrome://version/,找到 ChromeDriver 的路径,例如:C:\Program Files (x86)\Google\Chrome\Application\chrome.exe。 -
在 Python 中,使用 Remote WebDriver 连接已经打开的浏览器,代码如下:
from selenium import webdriver
# 设置 ChromeDriver 路径
chrome_driver_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
# 使用 Remote WebDriver 连接已经打开的浏览器
driver = webdriver.Remote(command_executor='http://127.0.0.1:9515', desired_capabilities=webdriver.DesiredCapabilities.CHROME)
其中,command_executor 参数指定了 ChromeDriver 的路径,desired_capabilities 参数指定了浏览器的配置信息,这里使用 CHROME 作为浏览器类型。
- 运行代码,即可连接已经打开的浏览器,继续进行后续操作。
原文地址: https://www.cveoy.top/t/topic/jpzP 著作权归作者所有。请勿转载和采集!