【发布时间】:2018-06-13 06:17:03
【问题描述】:
我预计会发生什么: 程序应该从列表中找到预期的 Web 元素,单击它,找到合同 ID 并与给定的合同 ID 匹配。如果是,则中断循环,否则单击返回按钮并继续,直到满足条件为止。
实际问题: 为每个循环运行它;程序在列表中找到第一个 Web 元素并通过第一个 if 条件。在单击 web 元素后,由于不满足第二个 if 条件,它会退出循环并再次检查每个循环,但程序或代码在此处中断并抛出错误,例如“stale element reference: element is not attach to页面文档":(
如何克服这个错误?
注意:-“我需要的 Web 元素在给定合同 ID 的列表中排在第三位”。
// Selenium Web Driver with Java-Code :-
WebElement membership = driver.findElement(By.xpath(".//*[@id='content_tab_memberships']/table[2]/tbody"));
List<WebElement> rownames = membership.findElements(By.xpath(".//tr[@class='status_active']/td[1]/a"));
// where rownames contains list of webElement with duplicate webElement names eg:- {SimpleMem, SimpleMem , SimpleMem ,Protata} but with unique contarct id (which is displayed after clicking webElement)
for (WebElement actual_element : rownames) {
String Memname = actual_element.getAttribute("innerHTML");
System.out.println("the membershipname"+ Memname);
if (Memname.equalsIgnoreCase(memname1)) {
actual_element.click();
String actualcontractid = cp.contarct_id.getText();
if (actualcontractid.equalsIgnoreCase(contractid)) {
break;
} else {
cp.Back_Btn.click();
Thread.sleep(1000L);
}
}
}
【问题讨论】:
-
您提到程序抛出错误,能否请您粘贴更新您的错误问题?我认为您可能面临 StaleElemenetReference - seleniumhq.org/exceptions/stale_element_reference.jsp