Selenium Optimize getVisibleElement (when an element is missing)

I am using jBehave / Selenium to test automation.

Now I use the following code to get the visible elements on the page;

public WebElement getVisibleElement( final By by, final WebElement parentElement, int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod ) {
       return fluentWait(timeoutValue, timeoutPeriod, pollingInterval, pollingPeriod).until( new Function<WebDriver, WebElement>(){
                public WebElement apply(WebDriver driver) {
                    try{
                        WebElement element = parentElement.findElement(by);
                        if ( element.isDisplayed() ) {
                            return element;
                        } else {
                            return null;
                        }
                    } catch( NoSuchElementException e ) {}

                    return null; 
                }
        } );
    }

Now the problem is that if an element is missing from the page, Selenium spends a lot of time trying to find it on the page. Is there a way that I can optimize the code so that it does not spend much time in such cases?

+4
source share
2 answers

First of all, I think you should not check for the presence of an element by calling findElement and detecting a potential exception. Better use findElements and check list size

public WebElement getElementIfPresent(By locator) {
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
    List<WebElement> elements = driver.findElements(locator);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    if(!elements.isEmpty()) {
        return elements.get(0);
    }
    else {
        return null;
    }
}

, WebDriver . , , , . , , . , .

isDisplayed() , .

0

- findElements(locator).size()...

    /**
     * Private method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
     */
    private WebElement waitForElement(By by) {
        int attempts = 0;
        int size = driver.findElements(by).size();

        while (size == 0) {
            size = driver.findElements(by).size();
            if (attempts == MAX_ATTEMPTS) fail(String.format("Could not find %s after %d seconds",
                                                             by.toString(),
                                                             MAX_ATTEMPTS));
            attempts++;
            try {
                Thread.sleep(1000); // sleep for 1 second.
            } catch (Exception x) {
                fail("Failed due to an exception during Thread.sleep!");
                x.printStackTrace();
            }
        }

        if (size > 1) System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");

        return driver.findElement(by);
    }

, , . - ,

WebElement myElement = waitforElement(By.cssSelector("input#someInput"));
myElement.sendKeys("something");

, .

0

All Articles