zoukankan      html  css  js  c++  java
  • (三十)WebDriver API之窗口截图

    随笔记录方便自己和同路人查阅。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

      学习selenium自动化之前,最好先学习HTML、CSS、JavaScript等知识,有助于理解定位及操作元素的原理。关于python和selenium安装请自行搜索别的资料,

    这里就不多做介绍了,所有例子均使用python3.6+selenium执行的。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

    窗口截图


      自动化用例是由程序去执行的,因此有时候打印的错误信息并不十分明显。如果在脚本执行出错的时候能对当前窗口截图保存,那么通过图片就可以非常直观地看出出错的原因,WebDriver提供了截图的函数get_screenshot_as_file()来截取当前窗口。

    from selenium import webdriver
    from time import sleep
    
    driver = webdriver.Chrome()
    driver.get('http://baidu.com')
    
    driver.find_element_by_id('kw').send_keys('selenium')
    driver.find_element_by_id('su').click()
    
    sleep(2)
    #截取当前窗口,并保存截图至指定位置
    driver.get_screenshot_as_file('d:\baidu.img.png)

      脚本运行完成后打开D盘,就可以找到baidu.img.png图片文件了。

  • 相关阅读:
    Azure Messaging-ServiceBus Messaging消息队列技术系列6-消息回执
    Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务
    Azure Messaging
    消息队列技术之基本概念
    Azure IoT 技术研究系列1
    Azure IoT 技术研究系列3
    Azure IoT 技术研究系列2
    Azure IoT 技术研究系列4
    memset()
    C++ GetComputerName()
  • 原文地址:https://www.cnblogs.com/lirongyang/p/11459797.html
Copyright © 2011-2022 走看看