繁体   English   中英

[量角器] [滚动]我正在尝试使用while循环滚动网页。 谁能帮助我了解代码有什么问题

[英][Protractor][Scroll] I am trying to scroll my web page using while loop. Can anyone help me understand what is wrong with code

调用此函数进行滚动,然后解析该函数​​会使浏览器进入休眠状态2秒钟。

   scrollToElement(webElement: any) {
        browser.executeScript('window.scrollTo(0,400);').then(()=>{
            console.log("sleeping"+ browser.sleep(2000));
        }).catch((err)=>{
            assert.fail("failed to scroll");
        }); 
    }

调用函数代码段:

while(!(arr[0]===arr[2])){
            console.log('scroll');
            utils.scrollToElement(this.scrollUpLink);
            countTop = utils.getTextfrom(this.getTotalcountOnTop);
             let arrIn=countTop.split(" ");
             arr[0]=arrIn[0];
             arr[2]=arrIn[2];
             console.log(" Indisde :"+arr[0]+ " "+ arr[2]);
        }

在这里,我正在检查两个字符串的相等性并滚动直到两个都不相同。

The output that I get is:
scroll
 Indisde :24 434
scroll
 Indisde :24 434
scroll
 Indisde :24 434
scroll
 Indisde :24 434

您的代码存在一些实现问题,我的意思是您没有正确处理Promise ,并且没有在window.scrollTo(0,400);增加400window.scrollTo(0,400);

而且您在then()中使用sleep() ,因此,以下是我根据您的代码尝试过的代码,它完全可以正常工作,因此请相应地更改代码并尝试运行:

// Declaring driver
var driver = null;

// Funtion to perform scroll
async function scrollToElement(xValue, yValue) {
    await driver.executeScript('window.scrollTo('+xValue+', '+yValue+');')
}

// Function which passes counter to a scrolling function and waits 2 seconds before doing the scroll 
async function doScroll() {
    // Initializing the driver
    driver = await browser.setup('chrome');
    // Launching the chrome and navigating to google.com
    await driver.get('http://www.google.com');
    // Searching something in the google
    await driver.findElement(By.name('q')).sendKeys('alicse3'+Key.ENTER);
    // Calling the scrolling funciton
    let i = 0, xValue = 0, yValue = 100;
    while(i++ <= 5) {
        await scrollToElement(xValue, yValue+=100);
        await console.log("=> The 'i' value is " + i);
        await driver.sleep(2000);
    }
}

// Executor function
doScroll();

希望对您有帮助...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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