这很好用:
driver.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();
但是当我为这样的表迭代它时失败了:
row.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();
HTML :
<table cellspacing="0" cellpadding="0" border="0" class="list">
<tbody>
<tr class="headerRow">
<th scope="col" class="actionColumn">Action</th>
<th class=" zen-deemphasize" scope="col"><a title="Customer Name - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary&srtClmn=ACCOUNT.NAME&srtOrd=1&relatedListId=Account','noh=1','Account','Sorting...');return false;"
href="">Customer Name</a></th>
<th class=" zen-deemphasize" scope="col"><a title="Customer Site - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary&srtClmn=ACCOUNT.SITE&srtOrd=1&relatedListId=Account','noh=1','Account','Sorting...');return false;"
href="">Customer Site</a></th>
<th class=" zen-deemphasize" scope="col"><a title="Work Phone - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary&srtClmn=00NE0000004a58n&srtOrd=1&relatedListId=Account','noh=1','Account','Sorting...');return false;"
href="">Work Phone</a></th>
<th class=" zen-deemphasize" scope="col"><a title="Customer Owner Alias - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&fpg=-1kpv5z603z3zv&str=edwin&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&initialViewMode=summary&srtClmn=CORE.USERS.ALIAS&srtOrd=1&relatedListId=Account','noh=1','Account','Sorting...');return false;"
href="">Customer Owner Alias</a></th>
</tr>
<!-- ListRow -->
<tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even first">
<td class="actionColumn"><a title="Edit - Record 1 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/001Q0000012jqBq/e" data-seclki="001Q0000012jqBq" data-seclkh="fb70693f5a4ab349f7bf6e74c4f8acff" data-seclke="Account"
class="actionLink" href="/001Q0000012jqBq/e?srPos=0&srKp=001&retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv">Edit</a></td>
<th
class=" dataCell " scope="row"><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/001Q0000012jqBq" data-seclki="001Q0000012jqBq" data-seclkh="fb70693f5a4ab349f7bf6e74c4f8acff" data-seclke="Account" href="/001Q0000012jqBq?srPos=0&srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell "> </td>
<td class=" dataCell ">(444) 444-4444</td>
<td class=" dataCell "><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000012jqBq" data-seclkh="fb70693f5a4ab349f7bf6e74c4f8acff" data-seclke="Account" href="/005E0000005pAqf?srPos=0&srKp=001">webapi</a></td>
</tr>
<!-- ListRow -->
<tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow odd">
<td class="actionColumn"><a title="Edit - Record 2 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="2" data-seclkp="/001Q0000013hzA7/e" data-seclki="001Q0000013hzA7" data-seclkh="b182c705723f5e1a6f828808c22c20f3" data-seclke="Account"
class="actionLink" href="/001Q0000013hzA7/e?srPos=1&srKp=001&retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv">Edit</a></td>
<th
class=" dataCell " scope="row"><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="2" data-seclkp="/001Q0000013hzA7" data-seclki="001Q0000013hzA7" data-seclkh="b182c705723f5e1a6f828808c22c20f3" data-seclke="Account" href="/001Q0000013hzA7?srPos=1&srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell "> </td>
<td class=" dataCell ">(435) 345-4543</td>
<td class=" dataCell "><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="2" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000013hzA7" data-seclkh="b182c705723f5e1a6f828808c22c20f3" data-seclke="Account" href="/005E0000005pAqf?srPos=1&srKp=001">webapi</a></td>
</tr>
<!-- ListRow -->
<tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even">
<td class="actionColumn"><a title="Edit - Record 3 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="3" data-seclkp="/001Q0000014lLNc/e" data-seclki="001Q0000014lLNc" data-seclkh="36ddf075be1199f1439c154f56aa01ee" data-seclke="Account"
class="actionLink" href="/001Q0000014lLNc/e?srPos=2&srKp=001&retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv">Edit</a></td>
<th
class=" dataCell " scope="row"><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="3" data-seclkp="/001Q0000014lLNc" data-seclki="001Q0000014lLNc" data-seclkh="36ddf075be1199f1439c154f56aa01ee" data-seclke="Account" href="/001Q0000014lLNc?srPos=2&srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell "> </td>
<td class=" dataCell ">(444) 444-4444</td>
<td class=" dataCell "><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="3" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000014lLNc" data-seclkh="36ddf075be1199f1439c154f56aa01ee" data-seclke="Account" href="/005E0000005pAqf?srPos=2&srKp=001">webapi</a></td>
</tr>
<!-- ListRow -->
<tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow odd">
<td class="actionColumn"><a title="Edit - Record 4 - Edwin,Nelson" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/001Q0000014jyLD/e" data-seclki="001Q0000014jyLD" data-seclkh="ebc0d5e11b734a7961e526f4db9f4ed1" data-seclke="Account"
class="actionLink" href="/001Q0000014jyLD/e?srPos=3&srKp=001&retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv">Edit</a></td>
<th
class=" dataCell " scope="row"><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/001Q0000014jyLD" data-seclki="001Q0000014jyLD" data-seclkh="ebc0d5e11b734a7961e526f4db9f4ed1" data-seclke="Account" href="/001Q0000014jyLD?srPos=3&srKp=001">Edwin,Nelson</a></th>
<td
class=" dataCell "> </td>
<td class=" dataCell ">(353) 242-3423</td>
<td class=" dataCell "><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000014jyLD" data-seclkh="ebc0d5e11b734a7961e526f4db9f4ed1" data-seclke="Account" href="/005E0000005pAqf?srPos=3&srKp=001">webapi</a></td>
</tr>
<!-- ListRow -->
<tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even last">
<td class="actionColumn"><a title="Edit - Record 5 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="5" data-seclkp="/001Q0000012m1Xb/e" data-seclki="001Q0000012m1Xb" data-seclkh="4226e6544070ec8c55fdc3fec27f099c" data-seclke="Account"
class="actionLink" href="/001Q0000012m1Xb/e?srPos=4&srKp=001&retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv">Edit</a></td>
<th
class=" dataCell " scope="row"><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="5" data-seclkp="/001Q0000012m1Xb" data-seclki="001Q0000012m1Xb" data-seclkh="4226e6544070ec8c55fdc3fec27f099c" data-seclke="Account" href="/001Q0000012m1Xb?srPos=4&srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell "> </td>
<td class=" dataCell ">(656) 757-5463</td>
<td class=" dataCell "><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="5" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000012m1Xb" data-seclkh="4226e6544070ec8c55fdc3fec27f099c" data-seclke="Account" href="/005E0000005pAqf?srPos=4&srKp=001">webapi</a></td>
</tr>
</tbody>
</table>
&#13;
Java代码:
// Grab the table
WebElement table = driver.findElement(By.xpath("/html/body/div[1]/div[2]/table/tbody/tr/td/div[2]/table/tbody/tr/td[2]/div[2]/div[2]/div[1]/div/div[2]/div/div[2]/table"));
System.out.println("table is selected");
// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
System.out.println("row is selected");
// And iterate over them, getting the cells
for (WebElement row : allRows)
{
System.out.println("in first loop");
row.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();//This is where am facing problem. The element has to be clicked for each row every iteration
System.out.println("element");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
scrollToApplications();
driver.navigate().back();
&#13;
不知道我哪里出错了。
任何帮助,将不胜感激。
感谢
答案 0 :(得分:1)
在每次点击后,您都会导航到其他页面,因此元素将变得陈旧。在这种情况下,您需要再次初始化webelements,否则驱动程序将抛出Stale Element引用异常。
在查找元素时也要避免使用绝对xpath。
尝试以下代码:
WebElement table = driver.findElement(By.xpath("//table"));
List<WebElement> allRows = table.findElements(By.tagName("tr"));
System.out.println(allRows.size());
// And iterate over them, getting the cells
for(int i=1;i<allRows.size();i++){
WebElement table1 = driver.findElement(By.xpath("//table"));
List<WebElement> allRows1 = table1.findElements(By.tagName("tr"));
allRows1.get(i).findElement(By.xpath(".//td[normalize-space()='Edit']/following-sibling::th/a")).click();
Thread.sleep(2000);
driver.navigate().back();
}
如果您有任何疑问,请告诉我
答案 1 :(得分:0)
当我们尝试找到关于行元素的Element时,我希望“//”不是必需的。如果您使用了这个,那么xpath会被更改,它将是一个新元素,但不是行的子元素。尝试改变路径
row.findElement(By.xpath("td[normalize-space()='Edit']/following-sibling::th/a")).click();
答案 2 :(得分:0)
请发布与HTML相关的源和错误消息,我认为这是因为xpath不正确。