首页 > 解决方案 > isDisplayed() 方法执行时间太长

问题描述

我正在研究Java Selenium项目并使用isDisplayed()方法来验证页面上是否显示某些元素,并且执行每个这样的方法大约需要45秒,有什么建议为什么它可能需要这么长时间,可以吗?在框架级别指定了一些等待?注意:不是我创建的框架,我只是在更新它。\

@FindBy(xpath = "//input[@id='productLIGrpTermDeal_chkPartySlf']//following-sibling::div")    
List<WebElement> GroupTermDealerLifeInsuranceSelf1;


public boolean verify_Icon_Is_Not_Displayed_() throws Exception {

    try {
        log.debug("Validate Icon is not Displayed");

        Assert.assertEquals(0, GroupTermDealerLifeInsuranceSelf1.size());

        System.out.println("Icon is not Displayed");
        log.info("Icon is validated successfully || Pass");
        return true;
    } catch (Exception e) {
        System.err.println("Icon is Displayed");
        log.error("Not able to Validate Icon is Displayed || Fail" + e.getMessage());
        return false;
    }
}

标签: javaseleniumselenium-webdriverwaitwebdriverwait

解决方案


您可以修改 XPath 查询以将List的大小限制为 1(或您在测试中使用的最大值)。可以通过在定位器末尾添加position() 函数来实现,例如:

@FindBy(xpath = "//input[@id='productLIGrpTermDeal_chkPartySlf']//following-sibling::div/*[position()<=1]")

一旦应用此更改,定位WebElement的时间将与DOM中的匹配数成比例减少。

更多信息:XPath 运算符和函数


推荐阅读