我希望发生的事情:程序应该从列表中找到预期的Web,点击它,找到合同id,并与给定的合同id匹配。如果是,请中断循环,否则单击“后退”按钮并继续执行,直到满足条件为止。
实际问题:为每个循环运行这个;程序在列表中找到第一个web元素,并传递第一个if条件。单击web元素后,作为第二个如果条件不满足的条件,它将再次退出循环并检查每个循环,但是程序或代码在这里中断并抛出错误,如“stale元素引用:元素未附加到页面文档":(
如何克服这个错误?
注意:-“在给定合同id的列表中,作为我所需的Web元素的第3位”。
// 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);
}
}
}发布于 2018-01-03 16:31:49
单击row元素后,您将从当前页面DOM导航。在新页上,如果合同Id不匹配,则将导航回上一页。
您期望能够从执行foreach循环时出现的列表行中访问元素。但是现在DOM被重新加载了,以前的元素是不可访问的,因此陈旧元素引用异常。
您能试一下下面的示例代码吗?
public WebElement getRowOnMatchingMemberNameAndContractID(String memberName, String contractId, int startFromRowNo){
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(int i=startFromRowNo; i<=rowNames.size(); i++){
String actualMemberName = rowNames.get(i).getAttribute("innerHTML");
if(actualMemberName.equalsIgnoreCase(memberName)){
rowNames.get(i).click();
String actualContractId = cp.contarct_id.getText();
if(actualContractId.equalsIgnoreCase(contractId)){
return rowNames.get(i);
}else {
cp.Back_Btn.click();
return getRowOnMatchingMemberNameAndContractID(i+1);
}
}
}
return null;
}我使用了递归和附加参数来处理先前单击的行。您可以使用0作为起始行调用上述方法,如-
WebElement row = getRowOnMatchingMemberNameAndContractID(expectedMemberName, expectedContractID,0);https://stackoverflow.com/questions/48079650
复制相似问题