<table width="100%" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<tr>
<tr>
<td class="tblForeground">
<table id="defaultaccess" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<tr>
<tr>
<tr>
<tr>
<td valign="TOP" align="LEFT" scope="rowgroup">
<img width="12" height="12" border="0" alt="" src="/img/8space.gif">
<img width="16" height="16" border="0" alt="Public Access" src="/img/publicaccess.gif">
<a target="info" href="/Livelink/livelink.exe?func=ll&objAction=EditAcl&objId=9388&id=9388&rightId=-1&permType=3&nextURL=%2FLivelink%2Flivelink%2Eexe%3Ffunc%3Dll%26objid%3D2000%26objAction%3Dbrowse%26sort%3Dname">Public Access</a>
在上面的表格html代码中,我想找到带有文字'Public Acess'的'a'标签。但是,当我使用下面的代码时,它显示错误,因为我无法找到该元素。以下是代码:
WebElement element1 = driver.findElement(By.className("tblForeground"));
List<WebElement> list1 = element.findElements(By.id("defaultaccess"));
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).findElement(By.tagName("a")).getText().equals("Public Access")) {
list1.get(i).findElement(By.tagName("a")).click();
break;
}
}
请帮助我找到上表中的特定元素。 提前谢谢。
答案 0 :(得分:0)
更改以下内容:
List<WebElement> list1 = element.findElements(By.id("defaultaccess"));
到
List<WebElement> list1 = element.findElements(By.xpath("//table[@id='defaultaccess']/tbody/tr/td"));
由于a
标记位于table -> tbody -> tr -> td
标记
答案 1 :(得分:0)
Helllo,试试这个:
int nr = driver.findElements(By.xpath("//td[contains(@class,\"tblForeground\")]/table/tbody/tr")).size();
for (int i = 1; i <= nr; i++)
{
try{
driver.findElement(By.xpath("//td[contains(@class,\"tblForeground\")]/table/tbody/tr[" + i + "]/td/a[contains(text(),\"Public Access\")]")).click();
} catch (NoSuchElementException e){
}
}