简体   繁体   English

isDisplayed()方法的执行时间过长

[英]isDisplayed() method execution time takes too long

I'm working on Java Selenium project and using isDisplayed() method to verify that some elements displayed or not displayed on the page, and execution of each method like that takes around 45 sec, is there any suggestions why it could take that long, can it be specified some waits on framework level? 我正在研究Java Selenium项目,并使用isDisplayed()方法来验证页面上显示或不显示的某些元素,并且每种方法的执行大约需要45秒,是否有任何建议说明为什么要花费这么长时间,是否可以在框架级别指定一些等待? Note: framework created not by me, I'm just updating it.\\ 注意:不是我创建的框架,我只是在更新它。\\

@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;
    }
}

You need to configure the driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 您需要配置driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); for implicit wait (Every Wait for finding element) in addition for a specific element wait you can use WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>))); 对于隐式等待(每次等待查找元素),除了特定元素等待之外,还可以使用WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>))); WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));

You can amend your XPath query to limit the size of the List to 1 (or whatever is the maximum value you're using in your tests). 您可以修改XPath查询,以将List的大小限制为1(或测试中使用的最大值)。 It can be achieved by adding position() function to the end of your locator like: 可以通过将position()函数添加到定位器的末尾来实现,例如:

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

Once you apply this change the time to locate the WebElements will be reduced proportionally to the number of matches in the DOM . 应用此更改后,定位WebElement的时间将与DOM中匹配项的数量成比例地减少。

More information: XPath Operators & Functions 详细信息: XPath运算符和函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM