06_Selenium Java元素定位

基本语法

driver.findElement(By.xxx)
driver.findElements(By.xxx)
策略语法语法描述
By iddriver.findElement(By.id())driver.findElements(By.id())通过id属性定位元素
By namedriver.findElement(By.name())driver.findElements(By.name())通过name属性定位元素
By class namedriver.findElement(By.className())driver.findElements(By.className())通过class属性定位元素
By tag namedriver.findElement(By.tagName())driver.findElements(By.tagName())通过HTML标签名定位元素
By link textdriver.findElement(By.linkText())driver.findElements(By.linkText())通过链接内容定位元素
By partial link textdriver.findElement(By.partialLinkText())driver.findElements(By.partialLinkText())通过部分链接内容定位元素
By cssdriver.findElement(By.cssSelector())driver.findElements(By.cssSelector())通过css选择器定位元素
By xpathdriver.findElement(By.Xpath())driver.findElements(By.Xpath())通过xpath定位元素

示例

By id

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

linkText官方文档描述
By LinkText示例

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:

  1. By id
    使用id,name,class属性是定位元素的首选方法。其中,用元素的id是最首选的方法,是最快速的策略。
    当发生下列情况时,无法使用id属性:
  • 不是所有的页面元素都拥有id属性
  • id属性的值是动态生成的
  1. findElement()方法定位元素时,会查询整个DOM,然后返回第一个匹配的元素
  2. 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"));
  1. NoSuchElementFoundException
    findElement()和findElements()方法找不到相应的元素时,会抛出该异常。

findElements:

Selenium WebDriver的findElements()方法,可以得到指定规则的集合,适用于需要在一组相似的元素上进行操作的情况。

List<WebElement> inputs = driver.findElements(By.tagName("input"))
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春暖花开dcm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值