selenium 使用已打开的chrome浏览器

首先说明下这样做的好处:就是你不用验证登陆状态,可以先登陆,再爬虫

1.先打开windows cmd 进入chrome安装目录,一般在C:\Program Files (x86)\Google\Chrome\Application下,然后运行

chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"

就打开了一个chrome浏览器,后面就是操作这个浏览器了,你可以先登陆,然后cookie,token什么的都有了

2.代码实现

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option(“debuggerAddress”, “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)
driver.get(‘https://me.csdn.net/ywah6666’)

chrome_driver 这个路径要根据 自己的安装路径来,注意这里chromedriver.exe 要去下载一个,给出淘宝的镜像吧,国外的要翻墙;还有端口要一致,可以随便定义未使用的端口

https://npm.taobao.org/mirrors/chromedriver

IE:

http://selenium-release.storage.googleapis.com/index.html

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值