selenium开始Chrom初始化

第一步查看谷歌浏览器版本下载对应的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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值