如何使用硒向下滚动屏幕?

问题描述 投票:0回答:2

我正在尝试用硒滚动屏幕以捕获某些元素,但我不能这样做...

我使用这个:

await driver.executeScript(“ window.scrollBy(0,450)”,“”);

和此:

常量页面=等待seleniumUtils.getElementByCSS('body');等待seleniumUtils.click(page);等待page.sendKeys(Key.COMMAND,Key.DOWN);

无效!我需要帮助,请:)

javascript selenium automation ui-automation qa
2个回答
0
投票

向下滚动:

WebDriver driver = new FirefoxDriver ();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript ("window.scrollBy (0,450)", "");

或,您可以执行以下操作:

jse.executeScript ("scroll (0, 450);");

向上滚动:

jse.executeScript ("window.scrollBy (0, -450)", "");

OR

jse.executeScript ("scroll (0, -450);");

0
投票

仅是一个python解决方案,可用于完全滚动无限滚动的网站。

随时进行编辑/重写以供使用...

步骤1:创建“滚动”功能

def scroll(driver, timeout):
    scroll_pause_time = timeout

    # Get scroll height
    last_height = driver.execute_script("return document.body.scrollHeight")

    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # Wait to load page
        time.sleep(scroll_pause_time)

        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            # If heights are the same it will exit the function
            break
        last_height = new_height

步骤2:一旦创建了“滚动”功能,就可以通过以下方式使用它:

from selenium import webdriver

driver = webdriver.Firefox() # create driver
driver.get('www.yoururl.com') # navigate url
scroll(driver, 5) # use scroll function to scroll the page every 5 second (usefull for infinite scrolling websites)
© www.soinside.com 2019 - 2024. All rights reserved.