python+selenium 控制已经打开的浏览器

初次使用selenium,之前根据网上教程一直无法控制已经打开的浏览器,主要是没有下载谷歌驱动。

谷歌驱动下载地址:http://npm.taobao.org/mirrors/chromedriver/

找到对应的版本,

python+selenium 控制已经打开的浏览器

我是在windows的开发环境,将压缩包里面的Chromedriver.exe 解压到谷歌浏览器同级目录

python+selenium 控制已经打开的浏览器

接下来就是设置快捷方式 更改端口,如果没有修改过也可以选择不设置

python+selenium 控制已经打开的浏览器

在目标那里加上  --remote-debugging-port=9222    当然用cmd命令启动也是可以的 。

然后测试是否接管成功。

import os
from selenium import webdriver
import datetime
import time
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
#chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_options.debugger_address="127.0.0.1:9222"
chrome_driver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
print(driver.title)

能够打印出title即可。