使用 Python 连接已经运行的浏览器

问题描述 投票:0回答:0

我想要一些帮助,看看是否有办法解决这个问题。 我正在测试制作一个可以连接到用户启动的浏览器的脚本。

我能为成功做些什么:

我可以连接到通过此命令启动的浏览器:

"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9244 --user-data-dir="C:\Users\user\OneDrive\Plocha\záložky\msedge1"

该命令必须有一个指定的用户数据目录,该目录必须与基本用户边缘数据不同。 (与此不同:C:\Users\user\AppData\Local\Microsoft\Edge\User Data)


我做不到的事:

目标是连接到用户启动的浏览器。所以我试图通过这个命令找到边缘进程的端口:

netstat -AB
。我复制带有进程名称的 IP 地址:msedge.exe 并将其粘贴到脚本中。它想了一分钟,得出了这个结果:https://i.stack.imgur.com/mGL4d.png


剧本:

import selenium
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service

edge_options = Options()
edge_options.add_experimental_option("debuggerAddress","127.0.0.1:55718")

edge_options.use_chromium = True
s = Service('C:/Users/WDAGUtilityAccount/Downloads/edgedriver_win64/msedgedriver.exe')
driver = webdriver.Edge(service = s, options = edge_options)

print(driver.title)
python selenium-webdriver microsoft-edge
© www.soinside.com 2019 - 2024. All rights reserved.