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

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()

http://www.xdnf.cn/news/10086.html

相关文章:

  • c++流之sstream/堆or优先队列的应用[1]
  • 智橙PLM与MES系统集成项目执行记录 智渤慧晟机械装备技术服务部 24.08
  • 大模型-attention汇总解析之-GQA
  • Python爬虫实战:研究Goutte库相关技术
  • Haproxy
  • 白皮精读:214页数据安全治理白皮书6.0【附全文阅读】
  • 超级对话3:大跨界且大综合的学问融智学应用场景述评(不同第三方的回应)之三
  • 低碳理念在道路工程中的应用-预制路面
  • P23:实现天气预测
  • 宽带不给公网IP?本地内网的网络服务怎么让外网访问?
  • [python] 最大公约数 和 最小公倍数
  • PostgreSQL日常运维
  • Linux | Shell脚本的常用命令
  • 计算机一次取数过程分析
  • AAAI 2025论文分享│STD-PLM:基于预训练语言模型的时空数据预测与补全方法
  • 八N皇后问题
  • 抗辐照加固CANFD芯片:以车规级设计提升商业航天系统可靠性
  • HCIP:MPLS静态LSP的配置及抓包
  • @Docker Compose部署Alertmanager
  • 基于Python的单斜式ADC建模与仿真分析
  • nginx日志分析笔记
  • 每日一题:H指数
  • Vue 3前沿生态整合:WebAssembly与TypeScript深度实践
  • systemctl实现定时任务(比crontab好用)
  • Python中的变量、赋值及函数的参数传递概要
  • ch12 课堂参考代码 及 题目参考思路
  • E. Melody 【CF1026 (Div. 2)】 (求欧拉路径之Hierholzer算法)
  • shadcn/ui
  • 探索智能仓颉:Cangjie Magic开发体验全记录
  • 昂瑞微在蓝牙亚洲大会上隆重推出新一代超低功耗蓝牙SoC芯片OM6627