Protractor: Scroll down

后端 未结 9 426
有刺的猬
有刺的猬 2020-11-30 04:59

I have an button on my page that is visible when the user scrolls down. Because of this, protractor tests give me an error:

UnknownError: unknown erro

相关标签:
9条回答
  • 2020-11-30 05:05

    The answer of this question was marked correct at top replied.But after upgrade the newest chrome v54.The following code maybe best solution.

    browser.actions().mouseMove(element).perform();
    
    0 讨论(0)
  • 2020-11-30 05:06

    I'd like to add to the previous answer, and hopefully provide a little more explanation.

    This code, in the call to 'executeScript':

    'window.scrollTo(0,0);'
    
    • Scrolls the window UP, to window coordinates 0,0...essentially to the very top
    • If you know the specific area you need to go to, just change the coordinates.

    If you know you want to be at the very bottom of the window, which was my goal. You can put a very large number in the 'y' coordinate, like I did here:

    browser.executeScript('window.scrollTo(0,10000);').then(function () {
        expect(<some control>.<some state>).toBe(<some outcome>);
    })
    
    0 讨论(0)
  • 2020-11-30 05:08

    The easiest way is scroll till the expected element and click it. Please use the below tested and verified code in the current protractor version .

    it('scroll to element', function() {
               browser.driver.get('https://www.seleniumeasy.com/');
                var btnSubscribe= element(by.id('mc-embedded-subscribe'));
                browser.executeScript("arguments[0].scrollIntoView();", btnSubscribe);
                 browser.sleep(7500);
                 btnSubscribe.click();
              });
    
    0 讨论(0)
  • 2020-11-30 05:11

    Other way, you can try this:

    this.setScrollPage = function (element) {
    
        function execScroll() {
            return browser.executeScript('arguments[0].scrollIntoView()',
                element.getWebElement())
        }
    
        browser.wait(execScroll, 5000);
    
    };
    
    0 讨论(0)
  • 2020-11-30 05:16

    In case anyone else is having troubles like I was:

    I was trying to scroll to the bottom of the page to load my next set of data in an infinite scroll scenario. I tried different variations of window.scrollTo as well as arguments[0].click() with no success.

    Finally I realized, to get the page to scroll, I had to bring focus to the "window" by clicking on any element within the window. Then window.scrollTo(0, document.body.scrollHeight) worked liked a charm!

    example code:

    element(by.className('<any element on page>')).click();
    browser.executeScript('window.scrollTo(0,document.body.scrollHeight)').then(function(){
    //whatever you need to check for here
    });
    
    0 讨论(0)
  • 2020-11-30 05:24

    If you are looking just to navigate to the top or bottom of a long page you could simply use the 'HOME' key to go too the top, or 'END' key to go to the bottom.

    e.g:

    browser.actions().sendKeys(protractor.Key.HOME).perform();
    

    or

    browser.actions().sendKeys(protractor.Key.END).perform();
    
    0 讨论(0)
提交回复
热议问题