0%

python | selenium 操纵已经打开的 chrome

通过调试接口来操作已经打开的 chrome

使用

1
chrome.exe --remote-debugging-port=9233 --user-data-dir="D:\chrome\1"

打开 chrome

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9233")
driver = webdriver.Chrome(options=options)

window_rect = driver.get_window_rect()

print("窗口左上角坐标:", window_rect["x"], window_rect["y"])
print("窗口右下角坐标:", window_rect["x"] + window_rect["width"], window_rect["y"] + window_rect["height"])

# 测试打印页面标题
print(driver.title)

ps: 我把 chromedriver.exe 放在同级目录中。

请我喝杯咖啡吧~