• 学习总结——Selenium元素定位


    读一本好书,不能读读就算了,做一下总结,变成自己的,以备查阅。

    1.         driver.findElement(By.id(<element ID>))

    ID是独一无二的,使用ID定位是最为推荐的方法。

    但是:1.不是所有元素都会指定ID;2.有的ID属性的值是动态生成的。

    2.         driver.findElement(By.name(<element name>))

    name属性不一定唯一,如果有多个,第一个出现的会被选择。

    3.         driver.findElement(By.className(<element class>))

    4.         driver.findElement(By.tagName(<htmltagname>))

    tagName()方法是定位 HTML 标记名称:

    WebElement table = driver.findElement(By.id("summaryTable"));

    List<WebElement> rows = table.findElements(By.tagName("tr"));

    assertEquals(10, rows.size());

    5.         driver.findElement(By.linkText(<linktext>))

    WebElement 类也可以支持查询子类元素。

    WebElement topLink = driver.findElement(By.id("div1")).findElement(By.linkText("top"));

    6.         driver.findElement(By.partialLinkText(<linktext>))

    当元素有部分在变时,可以用部分不变的内容来定位元素。

    7.         driver.findElement(By.cssSelector(<cssselector>))

    1)         绝对路径如:WebElement userName = driver.findElement(By.cssSelector("html >

             body > div > div > form > input"));

    但是,这个策略会有一些的限制,他取决于页面的整个结构。如果有些许改变,选择     器将找不到这个元素。

            相对路径

    2)         DOM中第一个<input>元素:WebElement userName = driver.findElement(By.cssSelector("input"));

    3)          使用class定位:先指定一个 HTML 的标签,然后加一个“.”符号,跟上 class 属性的值, WebElement loginButton =driver.findElement(By.cssSelector("input.login"));可以找到按钮的<input>标签 class 为 login 的元素。

      使用ID 来定位:先指定一个 HTML 标签,然后加上一个“#”符号,跟上 id 的属性  值,如下所示:

      WebElement userName =driver.findElement(By.cssSelector("input#username"));

             这将会返回 input 标签中 id 为 username 的元素。

      使用name定位:WebElement userName =

      driver.findElement(By.cssSelector("input[name=username]"));

             使用 name 属性来定位元素和直接用 By 类中的 name()方法来定位相似。     

             使用其他的属性定位:WebElement previousButton       driver.findElement(By.cssSelector("img[alt='Previous']"));

    4)         使用多个属性来定位<input>元素:WebElement previousButton =driver.findElement(By.cssSelector("input[type='submit'][value='Login']"));

    5)         使用属性名称定位元素:List<WebElement> imagesWithAlt =

             driver.findElements(By.cssSelector("img[alt]"));

      not()伪类匹配不满足规则的元素: 例如, 想要定位那些<img>标签中不含有alt属性,

             List<WebElement> imagesWithoutAlt=driver.findElements(By.cssSelector("img:not([alt])"));

    6)         部分属性值的匹配:

             input[id^= ' ctrl']:以ctrl开始

             input[id$='_userName']:以_userName结尾

             input[id*='userName']:包含userName

    8.         driver.findElement(By.xpath(<xpath queryexpression>))

    1)         绝对路径:WebElement userName =driver.findElement(By.xpath("html/body/div/div/form/input"));

             这个策略有局限性,他需要参考整个页面的文档结构。如改变了,此元素的定位将会

             失效。

      相对路径:

    2)         DOM中第一个<input>元素:

             WebElement userName = driver.findElement(By.xpath("//input"));

             使用索引来定位元素,第二个<input>

             WebElement passwd = driver.findElement(By.xpath("//input[2]"));

    3)         用 ID 属性来定位:

             WebElement userName =driver.findElement(By.xpath("//input[@id='username']"));

      使用 alt 属性来定位:

             WebElement previousButton = driver.findElement(By.xpath("img[@alt='Previous']"));

    4)         使用多个属性来定位<input>元素

    • WebElement previousButton =driver.findElement(By.xpath("//input[@type='submit'][@value='Login']"));
    • WebElement previousButton = driver.findElement(By.xpath("//input[@type='submit'and @value='Login']"));
    • WebElement previousButton = driver.findElement(By.xpath("//input[@type='submit'or @value='Login']"));

    5)         使用 XPath 及属性名称定位元素:

             List<WebElement> imagesWithAlt = driver.findElements(By.xpath ("img[@alt]"));

    6)         部分属性值的匹配:

             input[starts-with(@id,'ctrl')]:id以ctrl开始

             input[ends-with(@id,'_userName')]:id以_userName结束

             Input[contains(@id,'userName')]:id包含userName

    7)         使用值来匹配任意属性及元素:

             WebElement userName = driver.findElement(By.xpath("//input[@*='username']"));

    8)         使用 XPath 轴来定位元素:用到再研究。

     

    总结提示:

    1. 使用 id,name 或 class 属性是定位元素的首选方法。
    2. CSS 选择器和 XPath 在 Selenium 用户中非常流行,但是 CSS 选择器相比 XPath 从难易、速度、效率来说更为推荐大家使用。
    3. XPath 的查询慢于CSS 选择器,因为 XPath支持双向的查询。可以通过元素的父,兄弟,子节点来定位元素。
  • 相关阅读:
    matlab二维绘图学习摘要
    linux下有名管道进程通信
    《编写可读代码的艺术》第10章 抽取不相关的子问题
    《编写可读代码的艺术》第9章 变量可读性
    《编写可读代码的艺术》第8章 拆分超长的表达式
    《编写可读代码的艺术》第7章 简化循环和逻辑
    《编写可读代码的艺术》第6章 写出言简意赅的注释
    《编写可读代码的艺术》第5章 该写什么样的注释
    《编写可读代码的艺术》第4章 审美
    《编写可读代码的艺术》第3章 不会误解的名字
  • 原文地址:https://www.cnblogs.com/lucy-lily/p/5158144.html
Copyright © 2020-2023  润新知