如何通过除“类"之外的属性直接找到 WebElements和“名称"(例如“标题")
我是 Java 和 Selenium 的新手,所以如果我的问题听起来有点初级,我提前道歉.
我使用:
driverChrome.findElements(By.className("blabla"));
查找以blabla"作为其类名的元素,例如:
<span class="blabla" title="标题">...</span>
现在,如果我想通过其他属性查找所有元素怎么办?类似:
driverChrome.findElements(By.titleValue("the title"));
这是我目前用于执行此任务的代码:
列表spans = driverChrome.findElements(By.tagName("span"));for (WebElement we : spans) {if (we.getAttribute("title") != null) {if (we.getAttribute("title").equals("the title")) {...休息;}}}
但它并不快速且易于使用.
解决方案XPath
归档元素有很多方法1 绝对路径
<html><身体><表格><输入 id="demo"/></表格></div></身体><html>
获取输入"标签
xpath="/html/body/div/form/input"
2 相对路径
<html><身体><表格><输入 id="demo"/></表格></div></身体><html>
获取输入"标签
xpath="//输入"
3 索引
<html><身体><表格><输入id="demo1"/><输入id="demo2"></表格></div></身体><html>
获取输入'demo2'
xpath="//输入[1]"
4 任意单个属性
<html><身体><表格><输入id="demo1"/><输入 id="demo2" foo="bar"></表格></div></身体><html>
获取输入'demo2'
xpath="//input[@id='demo2']" (相当于By.id)
或者
xpath="//input[@foo='bar']"
5 任意多个属性
<html><身体><表格><输入id="1" type="提交"/><输入 id="2" foo="bar"/><input id="3" type="submit" foo="bar"/></表格></div></身体><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) 获取列表.否则你会得到第一个元素(如果你只使用 driver.findElement).因为所有 3 个输入元素都满足您的条件或",它给了您第一个.
6 包含属性
<html><身体><表格><输入id="1" type="提交"/><input id="2" foo="bar" daddy="dog"/><input id="3" type="submit" foo="bar"/></表格></div></身体><html>
获取第二个输入
xpath="//input[@daddy]"
因为只有第二个有属性'daddy'
7 内搜索
<html><身体><表格><input id="input1" daddy="dog"/><input id="input2" daddy="pig"/></表格></div><表格><input id="input3" daddy="dog"/><input id="input4" daddy="apple"/></表格></div></身体><html>
获取第二个div
xpath="//div[.//input[@daddy='dog'] 和 .//input[@daddy='apple']]"
总的来说,这些都是我现在可以解决的.希望对您有所帮助.
I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.
I use:
driverChrome.findElements(By.className("blabla"));
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.
解决方案There are many methods while archiving an element by XPath
1 Absolutely path
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="/html/body/div/form/input"
2 Relative path
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="//input"
3 Index
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
To get the input 'demo2'
xpath="//input[1]"
4 Arbitrary single attribute
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2" foo="bar">
</form>
</div>
</body>
<html>
To get input 'demo2'
xpath="//input[@id='demo2']" (equivalent to By.id)
Or
xpath="//input[@foo='bar']"
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>
To get 3rd input
xpath="//input[@type='submit'][@foo='bar']"
Or
xpath="//input[@type='submit' and @foo='bar']"
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.
6 Contains attribute
<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>
To get the second input
xpath="//input[@daddy]"
Because only the second one has attribute 'daddy'
7 Inner searching
<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>
To get the second div
xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
Overall those are all I can work out for now. Hope it helps.
相关文章