7 查找(定位)页面元素

不管做那种类型的UI自动化,其本质都是准确的操作测试对象(元素)。 那想要达到这个目的,我们就需要做两件事:

image.png

2个查找(定位)元素方法:

findElement()

返回一个WebElement元素

findElements()

返回一个List,多个WebElement元素

八种方式定位元素:

By.id(id):通过元素ID 属性查找
By.name(name):通过元素name属性查找
By.className(className) :通过元素classname属性查找
By.linkText(链接文本):通过链接文本
By.partialLinkText(部分链接文本):通过部分链接文本
By.cssSelector(Css路径):通过元素CSS路径
By.tagName(name):通过元素tagname查找
By.xpath(XPath路径):通过元素XPath查找


By.id("属性id值"):

HTML 源码:

<a onclick="return false;" id="lb" name="tj_login" href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F">登录</a>

代码例子:

  WebElement element = driver.findElement(By. id("lb")); 

注意:ID查找是最有效,也是最快的元素定位方式,如果一个元素有ID属性,那么定位这个元素最先考虑的就是通过属性ID来定位,而且通常ID在一个HTML中是唯一的值。


By.name("属性name值"):

HTML 源码:

<a class="reg" name="tj_reg" target="_blank" href="https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F">注册</a>

代码例子:

WebElement  element = driver.findElement(By. name("tj_reg")); 

By.className("属性class值"):

HTML 源码:

<a class="reg" name="tj_reg" target="_blank" href="https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F">注册</a>

代码例子:

WebElement  element = driver.findElement(By.className( "reg")); 

By. linkText ("Link文本"):

HTML 源码:

<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>

代码例子:

WebElement  element =  driver.findElement(By.linkText( "搜索设置" ));

By. partialLinkText("Link文本"):

HTML 源码:

<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>

代码例子:

WebElement  element =  driver.findElement(By. partialLinkText( "搜索" ));

By. tagName("dom节点名"):

HTML 源码:

<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a>

代码例子:

WebElement element=  driver.findElement(By. tagName( “a" ));

By. xpath("xpath路径"):

通过firepath 获取到页面元素的xpath路径:

image.png

代码例子:

WebElement element=  driver.findElement(By. xpath( “.//*[@id='kw']" ));

By. cssSelector("Css路径"):

跟xpath类似,通过firepath 获取到页面元素的css路径:

image.png

代码例子:

WebElement element=  driver.findElement(By. cssSelector(“#kw”));

注意:
1. 通过findElement()方法,不管使用哪种方式去查找(定位)元素,都需要注意,你通过这种方式查找出来的元素必须唯一,如果不唯一则可能报错也可能会默认找第一个元素。
2. findElements()同样支持这八种定位方式,只是获取的是多个元素,返回List
List<WebElement> elements = driver.findElements(By.name("test"));

3. 元素定位一定要定位到最终你要操作的元素上

Demo:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.List;

/**
 * Created by 米阳 on 2017/3/25.
 */
public class FindElementsTest {

    WebDriver driver;

    @BeforeMethod
    public void openChrome() {
        System.setProperty("webdriver.chrome.driver", "F:\\IdeaProjects\\SeleniumDemo1701\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    /**
     * 打开百度页面
     * 通过ID定位搜索文本框
     */
    @Test
    public void byIDTest() {
        driver.get("http://www.baidu.com");
        WebElement keyField = driver.findElement(By.id("kw"));
    }

    /**
     * 打开百度页面
     * 通过name定位搜索文本框
     */
    @Test
    public void byNameTest() {
        driver.get("http://www.baidu.com");
        WebElement keyField = driver.findElement(By.name("wd"));
    }

    /**
     * 打开百度页面
     * 通过class属性定位搜索文本框
     */
    @Test
    public void byClassTest() {
        driver.get("http://www.baidu.com");
        WebElement keyField = driver.findElement(By.className("quickdelete"));
    }


    /**
     * 打开百度页面
     * 通过linkText定位搜索文本框
     */
    @Test
    public void byLinkTextTest() {
        driver.get("http://www.baidu.com");
        WebElement keyField = driver.findElement(By.linkText("糯米"));
    }

    /**
     * 打开百度页面
     * 通过partialLinkText定位搜索文本框
     */
    @Test
    public void bypartialLinkTextTest() {
        driver.get("http://www.baidu.com");
        WebElement keyField = driver.findElement(By.partialLinkText("糯"));
    }

    /**
     * 打开百度页面
     * 通过tagname定位搜索文本框
     */
    @Test
    public void byTagName() {
        driver.get("http://www.baidu.com");
        List<WebElement> list = driver.findElements(By.tagName("input"));
        System.out.println(list.size());
    }


    /**
     * 打开百度页面
     * 通过xpathe 百度一下按钮
     */
    @Test
    public void byXpath() {
        driver.get("http://www.baidu.com");
        WebElement e1 = driver.findElement(By.xpath(".//*[@id='u1']/a[4]"));
        System.out.println(e1.getText());
    }

    /**
     * 打开百度页面
     * 通过xpath 百度一下按钮
     */
    @Test
    public void byXpath02() {
        driver.get("http://www.baidu.com");
        List<WebElement> list = driver.findElements(By.xpath(".//*[@id='u1']/a"));
        for (int i = 0; i < list.size(); i++) {
            String text = list.get(i).getText();
            System.out.println(text);
        }

    }

    /**
     * 打开百度页面
     * 通过cssSelector 百度图片
     */
    @Test
    public void byCSS() {
        driver.get("http://www.baidu.com");
        WebElement e1 = driver.findElement(By.cssSelector("#lg>img"));
    }

    @AfterMethod
    public void colsedBrowser() {
        driver.quit();
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在使用selenium webdriver进行元素定位时,通常使用findElement或findElements...
    不勤奋阅读 1,605评论 1 3
  • 在使用selenium webdriver进行元素定位时,通常使用findElement或findElements...
    宇文臭臭阅读 1,427评论 2 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,951评论 19 139
  • 上一次爬取网易云的个人专辑图片,这次来介绍WebDriver定位元素的方法。在社会上,我们要找一个人,首先要确定他...
    ccq_inori阅读 733评论 0 0
  • 门推开一掌宽就似乎被什么东西顶住了。 路德维希稍微用了一下力,未果。又狠狠推了一下,还是不行。 门内传来慌乱的碰碰...
    丹青客阅读 300评论 0 1