码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • python+selenium实现web自动化(基础入门)


    selenium 是一个自动化操控工具,支持对web端进行自动化操控,从而实现自动化测试。

    相关文档:

    • https://python-selenium-zh.readthedocs.io/zh-cn/latest/
    • https://www.selenium.dev/documentation/

    安装配置

    环境依赖:

    • python3.7+

    安装:

    $ pip install selenium
    
    • 1

    使用

    基本使用示例:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    # chrome 启动参数
    chrome_options = Options()
    # 禁用浏览器正在被自动化程序控制的提示
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])
    
    # 启动
    driver = webdriver.Chrome(options=chrome_options)
    
    # 跳转页面
    driver.get('https://blog.yiqiesuifeng.cn/')
    
    # 其他操作
    ele = driver.find_element_by_xpath('xpath 表达式') # 根据 xpath 获取元素对象
    ele.click() # 点击
    ele.input() # 输入
    ...
    
    # 关闭
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    selenium默认启动本机自带的chrome浏览器,如需指定浏览器路径,则可以这样:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    # 指定 chromedriver 路径
    chromedriver_path = r"chromedriver 路径"
    service = Service(executable_path=chromedriver_path)
    
    # chrome 启动参数
    chrome_options = Options()
    
    # 指定 chrome 路径
    chrome_path = r"chrome 路径"
    chrome_options.binary_location = chrome_path
    
    # 禁用浏览器正在被自动化程序控制的提示
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])
    
    # 启动
    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    # 跳转页面
    driver.get('https://blog.yiqiesuifeng.cn/')
    
    # 关闭
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    chromedriver版本需要支持对于chrome的版本

    • chromedriver下载地址:https://registry.npmmirror.com/binary.html?path=chromedriver/
    • chromium下载地址:https://registry.npmmirror.com/binary.html?path=chromium-browser-snapshots/
  • 相关阅读:
    关于小程序如何做到强制更新
    Android入门第14天-AndroidStudio本机开发环境中gradle、sdk以及AVD目录的迁移
    电商API接口汇总,引领企业国际化
    springboot基于Android的校园综合服务App平台的设计毕业设计源码181040
    文件相关的操作------Python
    伸缩升降料箱机器人HEGERLS A42T|灵活覆盖超广立体存储空间|大幅提升仓储利用率
    二维码怎么分解成链接?线上快速解码教学
    ElasticSearch 拼音插件elasticsearch-analysis-pinyin + IK 分词器
    EM聚类(下):用EM算法对王者荣耀英雄进行划分
    [Pyhton] SimPy 离散事件模拟框架详解 —— 以一个简单的汽车充电排队模拟为例
  • 原文地址:https://blog.csdn.net/John_rush/article/details/134521488
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号