• 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/
  • 相关阅读:
    信奥一本通1187:统计字符数
    嵌入式系统开发【深入浅出】 EXTI 与 NVIC
    centos安装git
    【Day32】LeetCode刷刷刷。[1700. 无法吃午餐的学生数量 ]
    JS数据类型的判断方式
    【日积月累】后端刷题日志
    母婴店做微信小程序开发的重要性
    Android:自定义列表弹窗的单选图标样式、去掉列表弹窗的上下提示线
    D. Bicolored RBS.
    项目持续集成配置流程
  • 原文地址:https://blog.csdn.net/John_rush/article/details/134521488