Selenium Java元素定位
基本语法
driver.findElement(By.xxx)
driver.findElements(By.xxx)
策略 | 语法 | 语法 | 描述 |
---|---|---|---|
By id | driver.findElement(By.id()) | driver.findElements(By.id()) | 通过id属性定位元素 |
By name | driver.findElement(By.name()) | driver.findElements(By.name()) | 通过name属性定位元素 |
By class name | driver.findElement(By.className()) | driver.findElements(By.className()) | 通过class属性定位元素 |
By tag name | driver.findElement(By.tagName()) | driver.findElements(By.tagName()) | 通过HTML标签名定位元素 |
By link text | driver.findElement(By.linkText()) | driver.findElements(By.linkText()) | 通过链接内容定位元素 |
By partial link text | driver.findElement(By.partialLinkText()) | driver.findElements(By.partialLinkText()) | 通过部分链接内容定位元素 |
By css | driver.findElement(By.cssSelector()) | driver.findElements(By.cssSelector()) | 通过css选择器定位元素 |
By xpath | driver.findElement(By.Xpath()) | driver.findElements(By.Xpath()) | 通过xpath定位元素 |
示例
By id
driver.findElement(By.id("kw"))
By name
driver.findElement(By.name("wd"))
By class name
driver.findElement(By.className("s_ipt"))
By tag name
driver.findElement(By.tagName("input"))
By link text
driver.findElement(By.linkText("新闻”))
By partial link text
driver.findElement(By.partialLinkText("123"))
By xpath
6种定位方式:(以百度的搜索框为例)
1.绝对路径
WebElement wElement = driver.findElement(By.xpath("/html/body/div/div/div/div/div/form/span/input"));
2.相对路径
WebElement wElement = driver.findElement(By.xpath("//form/span/input"));
3.利用元素属性定位
WebElement wElement = driver.findElement(By.xpath("//input[@id='kw']"));
WebElement wElement = driver.findElement(By.xpath("//*[@id='kw']"));
4.属性与层级结合
WebElement wElement = driver.findElement(By.xpath("//span[@class='bg s_ipt_wr quickdelete-wrap']/input"));
5.使用逻辑运算符
WebElement wElement = driver.findElement(By.xpath("//input[@class='s_ipt' and @id='kw']"));
6.使用部分属性值匹配
WebElement wElement = driver.findElement(By.xpath("//input[starts-with(@class,'s_')]"));//查找class属性中开始位置包含’s_’关键字的页面元素
WebElement wElement = driver.findElement(By.xpath("//input[contains(@class,'_i')]"));//查找class属性中包含_i关键字的页面元素
WebElement wElement = driver.findElement(By.xpath("//input[ends-with(@class,'pt')]"));//无效,ends-with是xpath2.0的语法,可能现在的浏览器还只支持1.0的语法
替换成:WebElement wElement = driver.findElement(By.xpath("//input[substring(@class, string-length(@class) - string-length('pt') +1) = 'pt']"));
WebElement wElement = driver.findElement(By.xpath("//input[contains(@class,'_i') and contains(@name,'wd')]"));
WebElement wElement = driver.findElement(By.xpath("//input[contains(@class,'_i') and @name='wd']"));
WebElement wElement = driver.findElement(By.xpath("//input[contains(@class,'_i') or contains(@name,'wd')]"));
还有一种text() 匹配的是显示文本信息
例如:<a href="http://news.baidu.com" target="_blank" class="mnav c-font-normal c-color-t">新闻</a>
WebElement wElement = driver.findElement(By.xpath("//a[text()='新闻']"));
WebElement wElement = driver.findElement(By.xpath("//a[contains(text(),'新')]"));
By css(7种方法)
1.通过class属性定位
WebElement wElement = driver.findElement(By.cssSelector(".s_ipt"));
2.通过id属性定位
WebElement wElement = driver.findElement(By.cssSelector("#kw"));
3.通过标签名定位
List<WebElement> inputs = driver.findElements(By.cssSelector("input"));
4.通过父子关系定位
List<WebElement> inputs = driver.findElements(By.cssSelector("span>input"));
5.通过属性定位
WebElement wElement = driver.findElement(By.cssSelector(“*[id=kw]"));
6.通配符
WebElement wElement = driver.findElement(By.cssSelector("[class$=_ipt]"));//选择每一个class属性的值以”_ipt"结尾的元素
7.组合定位
WebElement wElement = driver.findElement(By.cssSelector("form.fm>span>input.s_ipt"));
findElement:
- By id
使用id,name,class属性是定位元素的首选方法。其中,用元素的id是最首选的方法,是最快速的策略。
当发生下列情况时,无法使用id属性:
- 不是所有的页面元素都拥有id属性
- id属性的值是动态生成的
- findElement()方法定位元素时,会查询整个DOM,然后返回第一个匹配的元素
- WebElement类可支持查询子类元素。假设页面上有一些重复的元素,它们有不同的父元素。我们可以先定位其父元素,然后定位其子元素,方法如下:
WebElement father = driver.findElement(By.id("father"));
WebElement son = father.findElement(By.linkText("xxx"));
也可以将他们缩写成一行:
WebElement son = driver.findElement(By.id("father")).findElement(BylinkText("xxx"));
- NoSuchElementFoundException
findElement()和findElements()方法找不到相应的元素时,会抛出该异常。
findElements:
Selenium WebDriver的findElements()方法,可以得到指定规则的集合,适用于需要在一组相似的元素上进行操作的情况。
List<WebElement> inputs = driver.findElements(By.tagName("input"))