如何通过其属性直接查找WebElements,除了“class”之外和“名称” (例如“title”) [英] How to directly find WebElements by their attributes except "class" and "name" (for example "title")
问题描述
我是Java和Selenium的新人,所以如果我的问题听起来有些重要,我会提前道歉。
I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.
我使用:
driverChrome.findElements(By.className("blabla"));
查找具有blabla作为其className的元素,例如:
to find elements which have "blabla" as their className, for example:
<span class="blabla" title="the title">...</span>
现在,如果我想通过其他属性查找所有元素,该怎么办?类似于:
Now, what if I want to find all elements by their other attributes? something like:
driverChrome.findElements(By.titleValue("the title"));
这是我目前用来执行此任务的代码:
This is the code that I am currently using to do this task:
List<WebElement> spans = driverChrome.findElements(By.tagName("span"));
for (WebElement we : spans) {
if (we.getAttribute("title") != null) {
if (we.getAttribute("title").equals("the title")) {
...
break;
}
}
}
但它不快速且易于使用。
but it is not fast and easy to use.
推荐答案
通过 XPath归档元素时有很多方法
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
获取'输入'标记
xpath="/html/body/div/form/input"
2相对路径
2 Relative path
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
获取'输入'标记
xpath="//input"
3指数
3 Index
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
获取输入'demo2'
To get the input 'demo2'
xpath =// input [1]
xpath="//input[1]"
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2" foo="bar">
</form>
</div>
</body>
<html>
获取输入'demo2'
To get input 'demo2'
xpath="//input[@id='demo2']" (equivalent to By.id)
或
xpath="//input[@foo='bar']"
5任意多个属性
5 Arbitrary multiple attributes
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
获得第三次输入
xpath="//input[@type='submit'][@foo='bar']"
或
xpath="//input[@type='submit' and @foo='bar']"
如果使用xpath =// input [@ type ='submit'或@ foo ='bar']在这里你会得到一个数组。您可以通过driver.findElements(By.xpath(xpath))(java)获取List。否则你将得到第一个元素(如果你只使用driver.findElement)。因为所有3个输入元素都满足您的条件'或',它会为您提供第一个。
If use xpath="//input[@type='submit' or @foo='bar']" here you'll get an array. You can get the List by driver.findElements(By.xpath(xpath)) (java). Otherwise you'll get the first element(If you just use driver.findElement). Because all of the 3 input elements meet your condition 'or' and it gives you the first one.
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar" daddy="dog"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
获取第二个输入
xpath="//input[@daddy]"
因为只有第二个属性'爸爸'
Because only the second one has attribute 'daddy'
<html>
<body>
<div>
<form>
<input id="input1" daddy="dog" />
<input id="input2" daddy="pig"/>
</form>
</div>
<div>
<form>
<input id="input3" daddy="dog" />
<input id="input4" daddy="apple"/>
</form>
</div>
</body>
<html>
获得第二个div
xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
总的来说,这些都是我现在可以解决的问题。希望它有所帮助。
Overall those are all I can work out for now. Hope it helps.
这篇关于如何通过其属性直接查找WebElements,除了“class”之外和“名称” (例如“title”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!