【selenium操控手动打开的浏览器】

这里使用 chrome浏览器 来做示例。

整个下来主要有两个步骤,

手动打开浏览器,
使用 Python程序 去获取到手动打开的 chrome浏览器。
应用场景(理论上)
登录账号并且需要输入手机验证码的网站;
登录账号并且需要人机验证的网站(如图片点选、文字点选等人机验证;

1. 打开浏览器
首先来到安装 chrome浏览器 的文件夹下,例:C:\Program Files (x86)\Google\Chrome\Application。

在此界面打开 cmd窗口,

然后输入:chrome.exe --remote-debugging-port=9527 --user-data-dir=“F:\selenium\AutomationProfile” ,并回车。
这句代码的意思是启动 chrome浏览器 的调试模式,

user-data-dirr=“F:\selenium\AutomationProfile” 是在单独的配置文件中启动 chrome浏览器,可以理解为 新的浏览器,记得创建对应文件夹哦;
其中 9527 为端口号,可自行指定。

此时候,如果无误的话就可以看到桌面新打开了一个 chrome 浏览器了。

那接下来就是去控制这个 手动打开的 chrome浏览器 啦。

2. 编写 Python程序获取控制 浏览器
Demo代码 如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9527")
browser = webdriver.Chrome(options=options)
代码运行后,即获取 当前页面的 title 。


浏览器窗口请求了新的 URL(https://www.bilibili.com),并且获取到了当前页面的 title。

3. 总结
去到 chrome浏览器 安装的文件夹下,打开 cmd窗口,输入以下内容:

chrome.exe --remote-debugging-port=9527 --user-data-dir="F:\selenium\AutomationProfile"
1
然后执行 2. 的 Python代码即可控制当前浏览器窗口。

你可能感兴趣的:(chrome,selenium)