第一步查看谷歌浏览器版本下载对应的chromedriver
打开谷歌点击右上角三个点——帮助——关于Google Chrom可查看版本
下载chromedriver地址为CNPM Binaries Mirror
或者 Index of chromedriver-local
第一种方法
将以下内容保存为bat脚本,并放在Google 安装根目录
chrome.exe --remote-debugging-port=9222 --user-data-dir="D:\chrometemp"
为了平时方便测试,可以添加一个快捷方式到桌面
双击bat脚本发现打开终端和一个谷歌浏览器
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
options=webdriver.ChromeOptions()
options.debugger_address='127.0.0.1:9222'
driver=webdriver.Chrome(options=options)
driver.get('http://www.baidu.com')
运行之后就会发现之前打开的google脚本已经自动跳转到百度的首页了
第二种方法
终端中输入
"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir="D:\Python312\test"
C:\Program Files\Google\Chrome\Application\chrome.exe为自己谷歌的路径
"D:\Python312\test" 在可读写的磁盘中新建一个文件夹test也可以自己命名
idea中代码为
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
class BasePage():
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
# 配置 ChromeDriver 路径
driver_path = r"D:\Python312\chromedriver.exe"
# 创建服务对象
service = Service(executable_path=driver_path)
# 初始化浏览器驱动
driver = webdriver.Chrome(service=service, options=chrome_options)