zoukankan      html  css  js  c++  java
  • Python + selenium 实现自动网页操作 (一)

    selenium

    Selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera,Edge等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。

    准备工作

    1. 安装seleniumm
      pip install selenium

    2. 下载浏览器驱动

      Firefox浏览器驱动:geckodriver

      Chrome浏览器驱动:chromedriver

      需要把浏览器驱动放入系统路径中,或者直接告知selenuim的驱动路径
      3.测试代码

    
    from selenium import webdriver
    from time import sleep
    
    def main():
        # global driver
        chromedriver_path = "C:Program FilesGoogleChromeApplicationchromedriver.exe" #替换对应路径
        driver = webdriver.Chrome(executable_path="C:Program FilesGoogleChromeApplicationchromedriver.exe")  #替换对应路径
        # 打开页面
        page = driver.get('https://www.baidu.com/')
        sleep(100) # 暂停100秒,防止自动关闭
    
    if __name__ == "__main__":
        main()
    

    Selenium 教程

    官方文档:selenium

    百度搜索实例

    
    from selenium import webdriver
    from time import sleep
    
    def main():
        # global driver
        chromedriver_path = "C:Program FilesGoogleChromeApplicationchromedriver.exe"
        driver = webdriver.Chrome(executable_path="C:Program FilesGoogleChromeApplicationchromedriver.exe")
        # 打开页面
        page = driver.get('https://www.baidu.com/')
        sleep(1)
        search_input = driver.find_element_by_id('kw')
        search_input.send_keys("mac pro")
        btn = driver.find_element_by_id("su").click()
        # btn.click()
        sleep(100) # 暂停100秒,防止自动关闭
    
    if __name__ == "__main__":
        main()
    
  • 相关阅读:
    实验吧(你真的会PHP吗)CTF之代码审计最终版---解析是错的 我的才是对的
    php的intval函数
    memcached 查看所有的key
    kvm 启动libvirtd时出现错误
    mysql三种binlog日志的理解
    mysql连接提示1030
    执行curl -sSL 提示curl: (35) SSL connect error
    tomcat线程数查看
    docker 1.12.3版本搭建私有仓库,上传镜像报错:server gave HTTP response to HTTPS client”
    memcached安装
  • 原文地址:https://www.cnblogs.com/billyme/p/14660152.html
Copyright © 2011-2022 走看看