当前位置: 首页 > news >正文

selenium自动化浏览器

Selenium 是一个开源的 Web 应用程序自动化测试框架,主要用于模拟用户操作浏览器,支持功能测试、兼容性测试及管理任务自动化。

1.下载浏览器驱动

google:右上角三个点>设置>关于Chrome>查看版本>下载对应的webdriver

下载地址:Chrome for Testing availability

Edge:右上角三个点>设置>关于Microsoft Edge>查看版本>下载对应的webdriver

下载地址:Microsoft Edge WebDriver |Microsoft Edge 开发人员

2.示例代码

import timefrom selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeServiceservice = ChromeService(executable_path=r"chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)try:driver.get('https://www.baidu.com/index.htm')time.sleep(5)
except Exception as e:print(e)
finally:driver.quit()

元素定位使用Xpath

find_element(By.XPATH, "xpath_expression")  # xpath_expression使用浏览器F12,复制

扩展应用:

配合F12使用

import time
import pyperclip
import pyautogui
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECservice = ChromeService(executable_path=r"D:\desktop\se\driver\chromedriver.exe")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)try:driver.get('https://www.baidu.com/index.htm')time.sleep(3)# pyautogui.press('F12')# Ctrl + [ / Ctrl + ]  切换开发者工具面板pyautogui.hotkey('ctrl', 'shift', 'c')   # 'ctrl', 'shift', 'c' :元素检查模式       'ctrl', 'shift', 'j' :控制台(Console)time.sleep(2)for _ in range(3):  # 切换到网络pyautogui.hotkey('ctrl', ']')## WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "kw")))driver.find_element(By.ID,'kw').send_keys('大熊猫\n')time.sleep(1)pyautogui.hotkey('ctrl', 'f')# 复制文本到剪贴板pyperclip.copy("nicon_10750f3.png")# 粘贴内容pyautogui.hotkey('ctrl', 'v')pyautogui.press('enter')time.sleep(60)
except Exception as e:print(e)
finally:driver.quit()

相关文章:

  • JavaScript字符串方法全面指南:从基础到高级应用
  • 【软件设计】通过软件设计提高 Flash 的擦写次数
  • 003图书个性化推荐系统技术剖析:打造智能借阅新体验
  • 企业数字化转型的6大核心要素:从战略到落地的系统方法论
  • 对COM组件的调用返回错误 HRESULT E_FAIL
  • SAR ADC 比较器噪声分析(二)
  • Replacing iptables with eBPF in Kubernetes with Cilium
  • [特殊字符] 超强 Web React版 PDF 阅读器!支持分页、缩放、旋转、全屏、懒加载、缩略图!
  • Co-IP—验证蛋白互作的不二之选
  • MySQL之约束和表的增删查改
  • [STM32问题解决(2)]STM32通过串口与PC通信,打开串口助手后无法在打开状态下下载程序和复位STM32
  • Rerank的评测
  • 大模型-modelscope下载和使用chatglm3-6b模型
  • 【JUC】深入解析 JUC 并发编程:单例模式、懒汉模式、饿汉模式、及懒汉模式线程安全问题解析和使用 volatile 解决内存可见性问题与指令重排序问题
  • C++八股 —— 手撕线程池
  • Java限制单价小数位数方法
  • GitLens 教学(学习更新中)
  • 2025年渗透测试面试题总结-匿名[校招]红队攻防工程师(题目+回答)
  • 特伦斯 S75 电钢琴:重塑演奏美学的至臻之选
  • 数字化那点事系列文章
  • 中山精品网站建设新闻/十大互联网平台
  • 哪些网站可以做翻译兼职/百度投放广告怎么收费
  • 证券投资网站建设/查排名的网站
  • 网站建设托管/网站设计服务企业
  • 成都网站建设哪家/百度推广登录入口官网网
  • 小程序制作开发平台/seo博客网站