这是我的代码。
// Grab the table
WebElement table = driver.findElement(By.id("Account_body"));
// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
List<WebElement> cells = row.findElements(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a"));
for (WebElement cell : cells) {
// And so on
cell.click();
System.out.println("clicked row");
遍历所有行,并在表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?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I','/_ui/search/ui/UnifiedSearchResults?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&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?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I','/_ui/search/ui/UnifiedSearchResults?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&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?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I','/_ui/search/ui/UnifiedSearchResults?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&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?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I','/_ui/search/ui/UnifiedSearchResults?searchType=2&offset=0&fpg=11nkv2y5sf6ky&str=NELSON%2CEDWIN&sen=001&sen=003&sen=005&sen=a0H&sen=a08&sen=a07&sen=a0I&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%3FsearchType%3D2%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26str%3DNELSON%252CEDWIN">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 ">(888) 888-8888</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%3FsearchType%3D2%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26str%3DNELSON%252CEDWIN">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%3FsearchType%3D2%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26str%3DNELSON%252CEDWIN">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 last">
<td class="actionColumn"><a title="Edit - Record 4 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/001Q0000012m1Xb/e" data-seclki="001Q0000012m1Xb" data-seclkh="71e8dbf22eb142eef3edfd7d3576afae" data-seclke="Account"
class="actionLink" href="/001Q0000012m1Xb/e?srPos=3&srKp=001&retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3FsearchType%3D2%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26str%3DNELSON%252CEDWIN">Edit</a></td>
<th
class=" dataCell " scope="row"><a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/001Q0000012m1Xb" data-seclki="001Q0000012m1Xb" data-seclkh="71e8dbf22eb142eef3edfd7d3576afae" data-seclke="Account" href="/001Q0000012m1Xb?srPos=3&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="4" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000012m1Xb" data-seclkh="71e8dbf22eb142eef3edfd7d3576afae" data-seclke="Account" href="/005E0000005pAqf?srPos=3&srKp=001">webapi</a></td>
</tr>
</tbody>
</table>
答案 0 :(得分:0)
假设您的代码编写正确,并且关闭了正确的位置,以下是一些可能有用的想法。
首先,我没有看到带有table元素的id属性,所以如果这实际上是正确的元素,我会感到惊讶。
WebElement table = driver.findElement(By.id("Account_body"));
如果这不是正确的元素,那很有可能:
List<WebElement> allRows = table.findElements(By.tagName("tr"));
不是您期望的行。
第二,当您使用WebElement.findElement(By.xpath())时,在我看来,您应该将其与用于查找其他元素的元素进行对比。所以而不是:
By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")
看起来应该更像:
By.xpath("./td")
因为行的位置已经在您引用的表元素之下。
第三,如果你只是点击每一行的第二列,我希望你实际上不想点击第一行,因为那些只是标题,那么你不需要使用第二个foreach循环
第四,它真的是您感兴趣的第二列,还是您感兴趣的客户名称栏?
我认为像这样的东西应该可以工作,虽然你可能需要稍微使用xpaths,因为我没有尝试插入它们以查看它们是否与元素匹配。
WebElement table = driver.findElement(By.xpath(".//table[@class='list']"));
// Now get all the TR elements from the table but excludes the header row
List<WebElement> allRows = table.findElements(By.xpath(".//tr[not(@class='headerRow')"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
//assuming that you are actually trying to click on the link for each user name,
//and not just the second element
WebElement cell = row.findElement(By.xpath("./th/a");
// And so on
cell.click();
说实话,虽然我不知道这是否能解决你的问题。如果没有任何关于您无法点击第二列的信息,这就不过是猜测了。如果你可以包括代码失败的地方和异常错误,它将允许其他人给出更好的答案。