gpt4 book ai didi

java - 使用 Selenium 迭代表中的链接列表 - 第一次迭代工作正常,然后失败

转载 作者:行者123 更新时间:2023-12-02 13:21:52 25 4
gpt4 key购买 nike

这工作正常:

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&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=ACCOUNT.NAME&amp;srtOrd=1&amp;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&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=ACCOUNT.SITE&amp;srtOrd=1&amp;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&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=00NE0000004a58n&amp;srtOrd=1&amp;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&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=CORE.USERS.ALIAS&amp;srtOrd=1&amp;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&amp;srKp=001&amp;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&amp;srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell ">&nbsp;</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&amp;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&amp;srKp=001&amp;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&amp;srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell ">&nbsp;</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&amp;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&amp;srKp=001&amp;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&amp;srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell ">&nbsp;</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&amp;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&amp;srKp=001&amp;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&amp;srKp=001">Edwin,Nelson</a></th>
<td
class=" dataCell ">&nbsp;</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&amp;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&amp;srKp=001&amp;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&amp;srKp=001">NELSON,EDWIN</a></th>
<td
class=" dataCell ">&nbsp;</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&amp;srKp=001">webapi</a></td>
</tr>
</tbody>
</table>

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();

不知道我哪里出错了。
任何帮助,将不胜感激。
谢谢

最佳答案

每次点击后,您都会导航到其他页面,因此元素将变得陈旧。在这种情况下,您需要再次初始化 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();

}

如果您有任何疑问,请告诉我

关于java - 使用 Selenium 迭代表中的链接列表 - 第一次迭代工作正常,然后失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43537430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com