针对于selenium的一些常规配置(防检测,无头,保留自己浏览器的登录信息等)

在使用selenium的时候有时候会受到网站的检测导致我们的程序被迫中止,因此我们需要给selenium添加一些浏览器特征来防止被网站检测到**(1-4为防检测配置)**.

  1. 在给selenium添加参数的时候,我们可以使用add_argument
option = webdriver.ChromeOptions()  # 首先实例化一个ChromeOptions
  1. selenium添加user-agent参数
option.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
  1. 去除 “Chrome正受到自动化测试软件的控制”
option.add_experimental_option('excludeSwitches', ['enable-automation'])
  1. 添加浏览器特征
option.add_argument("--disable-blink-features=AutomationControlled")
  1. selenium无头模式(即不会弹出浏览器来运行)
option.add_argument('--headless')
  1. 保留自己浏览器的cookie信息等,让selenium操作的浏览器跟自己所使用的一样,而不是新开一个,在操作一些需要登录的网站的时候,可以直接免登陆
option.add_argument(r'--user-data-dir=C:\Users\honor\AppData\Local\Google\Chrome\User Data')
  1. 在新版的selenium中,在代码执行完之后浏览器就会自动关闭,但我们在调试的时候可能不需要它自动关闭,因此可以通过以下代码来让浏览器不自动关闭
option.add_experimental_option("detach", True)

而–user-data-dir=后面的值可以在谷歌浏览器(chrome://version/)中看到,替换为自己的即可
谷歌浏览器信息保存路径
在最后实例化driver对象的时候,将option添加到其中即可

driver = webdriver.Chrome(options=option)

感谢您的阅读!

  • 7
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Selenium 和 Edge 浏览器模拟扫码登录Python 代码示例: ```python from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 设置 Edge 浏览器的路径 edge_path = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" # 设置 EdgeOptions edge_options = webdriver.EdgeOptions() edge_options.use_chromium = True edge_options.add_argument("--headless") edge_options.add_argument("--disable-gpu") edge_options.add_argument("--disable-dev-shm-usage") edge_options.add_argument("--disable-extensions") edge_options.add_argument("--no-sandbox") edge_options.add_argument("--disable-browser-side-navigation") edge_options.add_argument("--disable-infobars") edge_options.add_argument("--disable-popup-blocking") edge_options.add_argument("--disable-default-apps") edge_options.add_argument("--disable-features=site-per-process") edge_options.add_argument("--disable-features=VizDisplayCompositor") edge_options.add_argument("--disable-features=RendererCodeIntegrity") edge_options.add_argument("--disable-features=NetworkServiceInProcess") edge_options.add_argument("--disable-features=NetworkService") # 创建 Edge 浏览器实例 driver = webdriver.Edge(executable_path=edge_path, options=edge_options) # 打开登录页面 driver.get("https://example.com/login") # 等待页面加载完成 wait = WebDriverWait(driver, 10) wait.until(EC.presence_of_element_located((By.ID, "qrcode"))) # 获取二维码元素 qrcode = driver.find_element_by_id("qrcode") # 鼠标悬停在二维码上,等待扫码登录 ActionChains(driver).move_to_element(qrcode).perform() # 等待登录成功 wait.until(EC.presence_of_element_located((By.CLASS_NAME, "success"))) # 获取登录成功后的页面标题 title = driver.title # 关闭浏览器 driver.quit() # 输出页面标题 print(title) ``` 希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值