简体   繁体   中英

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

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). It can be achieved by adding position() function to the end of your locator like:

@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 .

More information: XPath Operators & Functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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