zoukankan      html  css  js  c++  java
  • selenium + Python (1) -- 浏览器基本操作

    设置pycharm运行环境

    以下代码是在pycharm中运行的,虽然本地已经搭建了python+selenium环境,能在本地运行;但是在pycharm中运行时,要设置其解释器。

    浏览器基本操作

    # coding:utf-8
    # 第一步导入需要的模块
    from selenium import webdriver
    import time
    
    # 第二步打开浏览器
    driver = webdriver.Firefox()
    
    # 第三步打开百度
    driver.get("http://www.baidu.com")
    
    # 设置休眠时间3秒,也可是小数,单位是秒
    time.sleep(1)
    
    # 刷新页面
    driver.refresh()
    time.sleep(1)
    
    # 返回上一个页面
    driver.back()
    time.sleep(1)
    
    # 切换到下一个页面
    driver.forward()
    time.sleep(1)
    
    # 设置窗口大小
    driver.set_window_size(540, 960)
    time.sleep(1)
    
    # 将浏览器窗口最大化
    driver.maximize_window()
    time.sleep(1)
    
    # 截屏
    driver.get_screenshot_as_file("/screenshot/a1.png")
    time.sleep(1)
    
    # 退出, close用于关闭当前窗口,quit用于结束进程,关闭所有窗口,当测试结束时,要用quit
    driver.close()
    driver.quit()
    
  • 相关阅读:
    C#深入浅出 修饰符(二)
    HDU 5785 Interesting
    HDU 5783 Divide the Sequence
    HDU 5781 ATM Mechine
    UVA 714 Copying Books
    uva 1471 Defense Lines
    UVA 11134 Fabled Rooks
    UVA 11572 Unique Snowflakes
    UVA 11093 Just Finish it up
    UVA 10954 Add All
  • 原文地址:https://www.cnblogs.com/Jadie/p/9050367.html
Copyright © 2011-2022 走看看