selenium

简介

selenium使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动按照脚本代码做出点击,输入,打开,验证等操作,就像真实用户所做的一样,从终端用户的角度测试应用程序。

与python集成

from selenium import webdriver

driver = webdriver.Firefox()

driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
driver.quit()

selenium可以操纵各大主流浏览器chrome、firefox、ie等等,但需要下载相应的驱动包

chrome: http://chromedriver.storage.googleapis.com/index.html

firefox:https://github.com/mozilla/geckodriver/releases/

ie:http://selenium-release.storage.googleapis.com/index.html

webdriver(即:浏览器对象)基本使用方法

  1. 打开关闭标签页

     #打开
    def get(self, url) #关闭
    def close(self) #退出浏览器
    def quit(self)
  2. 设置浏览器宽高

     def set_window_size(self, width, height, windowHandle='current'):
    """
    Sets the width and height of the current window. (window.resizeTo) :Args:
    - width: the width in pixels to set the window to
    - height: the height in pixels to set the window to :Usage:
    driver.set_window_size(800,600)
    """
  3. 对象定位

    #通过id方式定位

    driver.find_element_by_id("kw")

     #通过name方式定位
    driver.find_element_by_name("wd") #通过tag name方式定位
    driver.find_element_by_tag_name("input") #通过class name 方式定位
    driver.find_element_by_class_name("s_ipt") #通过CSS方式定位
    driver.find_element_by_css_selector("#kw") #通过xphan方式定位
    driver.find_element_by_xpath("//input[@id='kw']") #通过link方式定位
    driver.find_element_by_link_text("贴 吧") #Partial Link Text 定位
    driver.find_element_by_partial_link_text("贴") #通过by指定方法类型定位
    driver.find_element(By.ID, 'foo')
  4. 定位一组元素

     #与上面类似加上s,但上面会抛出NoSuchElementException,下面找不到则返回empty list
    #通过by指定方法类型定位
    driver.find_elements(By.ID, 'foo')
  5. 框架和窗口定位

     def switch_to(self):
    """
    :Returns:
    - SwitchTo: an object containing all options to switch focus into :Usage:
    element = driver.switch_to.active_element
    alert = driver.switch_to.alert
    driver.switch_to.default_content()
    driver.switch_to.frame('frame_name')
    driver.switch_to.frame(1)
    driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
    driver.switch_to.parent_frame()
    driver.switch_to.window('main')
    """
  6. 执行js

     def execute_script(self, script, *args):
    """
    Synchronously Executes JavaScript in the current window/frame. :Args:
    - script: The JavaScript to execute.
    - \*args: Any applicable arguments for your JavaScript. :Usage:
    driver.execute_script('return document.title;')
    """ def execute_async_script(self, script, *args):
    """
    Asynchronously Executes JavaScript in the current window/frame. :Args:
    - script: The JavaScript to execute.
    - \*args: Any applicable arguments for your JavaScript. :Usage:
    script = "var callback = arguments[arguments.length - 1]; " \
    "window.setTimeout(function(){ callback('timeout') }, 3000);"
    driver.execute_async_script(script)
    """

webelement(元素)基本使用方法

  1. 点击

     driver.find_element_by_id("su").click()
    driver.find_element_by_id("su").submit()
  2. 输入文本

     driver.find_element_by_id("kw").send_keys("xxx")
  3. 获取属性/文本

     driver.find_element_by_id("kw").text()
    driver.find_element_by_id("kw").get_attribute()
    driver.find_element_by_id("kw").get_property()
  4. 层次定位

     #与webdiriver操作一样,可以以当前元素为父元素查找子元素
    parent = driver.find_element(By.ID, 'parent')
    parent.find_element(By.ID, 'child')

最新文章

  1. 25. Valid Palindrome
  2. java <? super Fruit>与<? extends Fruit>
  3. CF Amr and Pins (数学)
  4. weblogic集群无法启动,提示java.lang.NumberFormatException
  5. PRINTDLG 结构体
  6. Nginx学习——http配置项解析编程
  7. javascript中toString和valueOf方法的区别
  8. 批处理(Batch)---批处理脚本。
  9. [Inside HotSpot] Visual Studio2017编译调试openjdk12
  10. Active Directory Replication(复制)
  11. Linux 下面 Sqlserver 2017 的简单安装
  12. 搭建Hexo博客(一)-创建Hexo环境
  13. Kubernetes持久化存储1——示例
  14. 为何time_before 起作用【转】
  15. hbase与hive集成:hive读取hbase中数据
  16. JAVA基础知识总结:一到二十二全部总结
  17. [UE4]自定义结构体、类、数据表
  18. 移动端Css初始化
  19. poj2686 状压dp入门
  20. Java虚拟机的内存管理

热门文章

  1. Ueditor富文本添加视频内容,视频不显示以及编辑富文本时,视频不显示解决方案
  2. iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3306 -j DNAT --to-destination 172.17.0.2:3306 ! -i docker0: iptables: No chain/target/match by that name
  3. day18__文件操作
  4. [javascript]js原型链以及原型链继承
  5. Eclipse配置maven环境1
  6. windows 下搭建 MQTT 服务
  7. [CEOI1999]Parity Game 题解
  8. Wooden Stricks——两个递增条件的线性DP
  9. HDU 3686 Traffic Real Time Query System (图论)
  10. day78 通过axios实现数据请求