【问题标题】:How to iterate for each loop for duplicate values如何为每个循环迭代重复值
【发布时间】: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);

        }
    }
}

【问题讨论】:

标签: java selenium foreach


【解决方案1】:

点击row 元素后,您将离开当前页面DOM。在新页面上,如果 Contract Id 不匹配,您将导航回上一页。

您期望您应该能够从之前执行foreach 循环时出现的列表[rows] 中访问元素。但是现在重新加载了 DOM,更早的元素无法访问,因此 Stale Element Reference Exception

你能试试下面的示例代码吗?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 2021-04-13
    • 1970-01-01
    • 2021-10-29
    • 2013-01-29
    • 2012-05-19
    • 1970-01-01
    • 2017-04-28
    相关资源
    最近更新 更多