Selenium hides web element behind navigation bar

0

I have a problem with writing a value into an input element.

screenshot

When Selenium appeals to this input field, the page scrolls to the top and the input hides behind the navigation bar.

Before appealing, I execute this code:

int elementPosition = element.getLocation().getY();
String js = String.format("window.scroll(0, %s)", elementPosition-90);
((JavascriptExecutor)driver).executeScript(js);

But even after my scroll, the input is still hidden.

Also, I tried to set a value to this field through JavaScript:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('value', '"+date+"')",element);

Unfortunately, this does not help either.

Environment: Selenium 3.6.0 ver, FireFox 66 ver.

Igor Vlasuyk

Posted 2019-04-22T14:15:08.413

Reputation: 1

Answers

0

I use the following pair of methods to scroll into view:

/**
 * Wait until JQuery is inactive
 * @author Bill Hileman
 */
public void waitForJQueryToBeInactive() {

    Boolean isJqueryUsed = (Boolean) ((JavascriptExecutor) driver)
            .executeScript("return (typeof(jQuery) != 'undefined')");

    if (isJqueryUsed) {
        while (true) {
            // JavaScript test to verify jQuery is active or not
            Boolean ajaxIsComplete = (Boolean) (((JavascriptExecutor) driver)
                    .executeScript("return jQuery.active == 0"));
            if (ajaxIsComplete)
                break;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

}

/** 
 * Scroll a web element into view
 * @author Bill Hileman
 * @param ele - WebElement
 */
public void scrollIntoView(WebElement ele) {

    ((JavascriptExecutor)driver).executeScript("window.scrollTo(" + ele.getLocation().x + 
                                                                "," + ele.getLocation().y + ")");
    waitForJQueryToBeInactive();

}

Bill Hileman

Posted 2019-04-22T14:15:08.413

Reputation: 757

0

The website I test for is written in an ExtJS single page type of web page. When elements are not in view, I am still able to use the .SendKeys() method to type into the box. It happens real quick, but the box I'm working with comes into focus on the screen, without any issue.

If you put a thread.sleep(); right after the .SendKeys() statement, you should see the box you are interacting with in a visible location on the page.

Dan Pearman

Posted 2019-04-22T14:15:08.413

Reputation: 1