use*_*141 11 html java selenium
HTML代码如下所示:
<td id="id26a" class="doclisting-name link" style="width: 319px; min-width: 319px;">
<span id="id26b" title="Document">
<span class="ie-fallback-marker">
words
</span></span></td>
Run Code Online (Sandbox Code Playgroud)
我无法搜索元素ID,因为它一直在变化.我无法搜索元素的类,因为有多个可能会改变位置.
我希望能够点击SPAN标签之间的"WORDS" .这是可能吗?
这是我到目前为止使用的,但似乎都不起作用:
//string document is words.
public void testscenario123(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.linkText(document)).click();
}
Run Code Online (Sandbox Code Playgroud)
要么
//string document is words.
public void testscenario124(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.xpath("//*[contains(@span,'"+document+"')]")).click();
}
Run Code Online (Sandbox Code Playgroud)
您可以通过文本选择xpath,而normalize-space会去除空白:
//string document is words.
public void testscenario124(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.xpath("//*[normalize-space()='"+document+"']")).click();
}
Run Code Online (Sandbox Code Playgroud)
您可能还希望考虑等待元素出现,而不是声明显式睡眠
| 归档时间: |
|
| 查看次数: |
26129 次 |
| 最近记录: |