Lov*_*ava 3 java testing selenium selenium-chromedriver selenium-webdriver
我是Java和Selenium的新手,所以如果我的问题听起来有些重要,我会提前道歉.
我用:
driverChrome.findElements(By.className("blabla"));
Run Code Online (Sandbox Code Playgroud)
查找具有"blabla"作为其className的元素,例如:
<span class="blabla" title="the title">...</span>
Run Code Online (Sandbox Code Playgroud)
现在,如果我想通过其他属性查找所有元素,该怎么办?就像是:
driverChrome.findElements(By.titleValue("the title"));
Run Code Online (Sandbox Code Playgroud)
这是我目前用来执行此任务的代码:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它并不快速且易于使用.
通过XPath归档元素时有很多方法
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
Run Code Online (Sandbox Code Playgroud)
获取"输入"标签
xpath="/html/body/div/form/input"
Run Code Online (Sandbox Code Playgroud)
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
Run Code Online (Sandbox Code Playgroud)
获取"输入"标签
xpath="//input"
Run Code Online (Sandbox Code Playgroud)
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
Run Code Online (Sandbox Code Playgroud)
获取输入'demo2'
的xpath = "//输入[1]"
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2" foo="bar">
</form>
</div>
</body>
<html>
Run Code Online (Sandbox Code Playgroud)
获取输入'demo2'
xpath="//input[@id='demo2']" (equivalent to By.id)
Run Code Online (Sandbox Code Playgroud)
要么
xpath="//input[@foo='bar']"
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
获得第三个输入
xpath="//input[@type='submit'][@foo='bar']"
Run Code Online (Sandbox Code Playgroud)
要么
xpath="//input[@type='submit' and @foo='bar']"
Run Code Online (Sandbox Code Playgroud)
如果使用xpath ="// input [@ type ='submit'或@ foo ='bar']",那么你将获得一个数组.您可以通过driver.findElements(By.xpath(xpath))(java)获取List.否则你将获得第一个元素(如果你只使用driver.findElement).因为所有3个输入元素都符合你的条件'或',它给你第一个.
<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>
Run Code Online (Sandbox Code Playgroud)
获得第二个输入
xpath="//input[@daddy]"
Run Code Online (Sandbox Code Playgroud)
因为只有第二个属性'爸爸'
<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>
Run Code Online (Sandbox Code Playgroud)
获得第二个div
xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
Run Code Online (Sandbox Code Playgroud)
总的来说,这些都是我现在可以解决的问题.希望能帮助到你.
| 归档时间: |
|
| 查看次数: |
17868 次 |
| 最近记录: |