三、WebDriver定位元素

推荐使用的webdriver是firefox,因为他的firebug更能可以帮助定位页面元素使用

# create a new Firefox session

cls.driver = webdriver.Firefox()

1、通过id定位

可以通过find_element_by_id()方法来定位元素

#通过id定位

self.driver.find_element_by_id("hl").click()

self.driver.find_element_by_id("m1").click()

2、通过name定位

可以通过find_element_by_name()方法来定位元素

#通过name定位

self.driver.find_element_by_name("panel").click()

self.driver.find_element_by_name("find").click()

3、通过class定位

可以通过find_element_by_class_name()方法来定位元素

#通过class定位

self.driver.find_element_by_ class_name("panel panel-default").click()

self.driver.find_element_by_ class_name("panel-body").click()

4、通过tag定位

可以通过find_element_by_tag_name()方法来定位元素

#通过tag定位

self.driver.find_element_by_ tag_name("input").click()

self.driver.find_element_by_ tag_name("tag_name").click()

5、通过link定位

可以通过find_element_by_link_text()方法来定位元素

#link定位

self.driver.find_element_by_link_text("新闻").click()

self.driver.find_element_by_ link_text("贴吧").click()

6、通过XPath定位

可以通过find_element_by_xpath()方法来定位元素,其中包括绝对路径定位、元素属性定位、层级与属性结合定位、逻辑运算符定位

#绝对路径定位

self.driver.find_element_by_xpath("/div/div[2]/div/div/div[2]/div").clear()

#元素属性定位

self.assertEqual(self.driver.find_element_by_xpath("//div[@class='modal-content']//div[@class='modal-header']//div[@class='bootstrap-dialog-header']//div[@class='bootstrap-dialog-title']").text,u"插入链接")

#层级与属性结合定位

self.driver.find_element_by_xpath("//div[@class='editor-tool-bar']/div[2]/button").send_keys('C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg')

#逻辑运算符定位

self.driver.find_element_by_xpath("//div[@class='form-group' and @id=’form’]//input[@id='ILD_linkText']").send_keys(u"贵金属主站")

7、通过CSS定位(略)

8、通过By定位(略)

总结Selenium自动化测试方法(三)WebDriver定位元素方法的更多相关文章

  1. WebDriver定位元素方法

    如果把页面上的元素看作人的话,在现实世界如何找到某人呢?方法有三: 一.通过人本身的属性,例如他的姓名,手机号,身份证号,性别,这些可区别他人的属性.在web页面上的元素也有这些属性,例如,id.na ...

  2. 总结Selenium自动化测试方法(一)自动化测试基础

    总结Selenium自动化测试方法 一.自动化测试基础 1.什么样的项目适合自动化测试 ①任务测试明确,不会频繁变动 ②每日构建后的测试验证 ③比较频繁的回归测试 ④软件系统界面稳定.变动少 ⑤需要在 ...

  3. selenium+python定位元素方法

    定位元素方法 官网地址:http://selenium-python.readthedocs.org/locating-elements.html        这里有各种策略用于定位网页中的元素(l ...

  4. Selenium Webdriver定位元素的几种方式

    原文:http://www.cnblogs.com/tobecrazy/p/4570494.html 工作中使用到记录一下. 主要有: 上传 alter dialog prompt dialog co ...

  5. python selenium自动化(三)Chrome Webdriver的兼容

    当一个自动化测试被实现在一个浏览器之后,我们会希望我们的测试能够覆盖到尽量多的别的浏览器.通过跨平台的测试来保证我们的程序在多个浏览器下都能正常工作. 在安装了selenium之后,firefox w ...

  6. selenium中的webdriver定位元素失败的常见原因

    自动化测试中经常会出现无法定位元素的情况,报selenium.common.exceptions.NoSuchElementException错误 Frame/Iframe原因定位不到元素: 这个是最 ...

  7. ui自动化之selenium操作(二)定位元素-简单操作

    1. 将浏览器最大化 这里拿chrome举例,但是我在执行的时候一直报错,被坑了好久; 解决办法: 这是因为chromedriver是和chrome一一对应的,不兼容的版本就会报错: 所有chrome ...

  8. WebDriver的定位元素方法

    如果把页面上的元素看作人的话,在现实世界如何找到某人呢?方法有三: 一.通过人本身的属性,例如他的姓名,手机号,身份证号,性别,这些可区别他人的属性.在web页面上的元素也有这些属性,例如,id.na ...

  9. 解决Selenium弹出新页面无法定位元素问题(Unable to locate element)

    Python 2.7 IDE Pycharm 5.0.3 环境细节详见Python+Selenium+PIL+Tesseract真正自动识别验证码进行一键登录 对于同一页面无法定位元素问题请见姊妹篇解 ...

随机推荐

  1. 剑指offer--3题

    题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2, 3, ...

  2. Winform跨线程操作界面的策略

    BeginInvoke(new ThreadStart(() => toolStripButton1.Text = "aaa")); 1.非跨线程操作和部分跨线程get不会引 ...

  3. 用HAProxy和KeepAlived构建高可用的反向代理

      用HAProxy和KeepAlived构建高可用的反向代理 用HAProxy和KeepAlived构建高可用的反向代理 前言对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求 ...

  4. ObjectStore onFetch方法获取记录总数

    转自:http://blog.csdn.net/earthhour/article/details/38686029 ObjectStore onFetch方法获取记录总数 require(['doj ...

  5. javascript设计模式--备忘录模式(Memento)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. python模拟shell

    import fileinput import readline raw_input(xxx) exec filepinput.input

  7. ID3决策树---Java

    1)熵与信息增益: 2)以下是实现代码: //import java.awt.color.ICC_ColorSpace; import java.io.*; import java.util.Arra ...

  8. LA 3713

    The Bandulu Space Agency (BSA) has plans for the following three space missions: Mission A: Landing ...

  9. Android 中建立一个OpenGL ES的开发环境

    转自: http://wiki.eoe.cn/page/Building_an_OpenGL_ES_Environment.html 负责人:zhangql原文链接:http://docs.eoean ...

  10. linux read命令详解

    read命令从键盘读取变量的值,通常用在shell脚本中与用户进行交互的场合.该命令可以一次读取多个变量的值,变量和输入的值都需要使用空格隔开. 语法 read(选项)(参数) 选项 -p:指定读取值 ...