selenium 无头,edge浏览器 模拟扫码登录代码
时间: 2023-04-03 07:01:09 浏览: 204
以下是使用 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)
```
希望能对你有所帮助!
阅读全文