简体   繁体   中英

Protractor scrolling through executeScript not working

I am testing my Ionic app.

In one page, the button to be clicked is out of the bounds of the window. Hence the following code:

element.all(by.css('.item.item-complex')).get(9).click();

throws the error:

ElementNotVisibleError: element not visible

Hence, I am trying to scroll down the page to make the button visible in page and then try emulating the click on it. I am using the following code:

browser.executeScript('window.scrollTo(0, 200);').then(function() {
    element.all(by.css('.item.item-complex')).get(9).click();
    expect(browser.getTitle()).toEqual('Vegeta The Prince');
});

But the scrolling is not happening with the above code. Please help!

I am using Google Chrome.

When I encounter issues like this, I scroll into view :

var elm = element.all(by.css('.item.item-complex')).get(9);
browser.executeScript("arguments[0].scrollIntoView();", elm.getWebElement());

elm.click();

I solved this issue using

window.scrollTo(x, y)

code:

var elm = element.all(by.css('.item.item-complex')).get(9);

elm.getLocation()
    .then(function (location) {
        return browser.executeScript('window.scrollTo(' + location.x + ', ' + location.y + ');')
    })
    .then(function () {
        return elm.click();
    })
})

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