首页 > 解决方案 > Selenium - 等待 ScrollIntoView 完成

问题描述

我需要滚动到一个元素并截取页面的屏幕截图,但驱动程序在页面完全滚动到该元素之前截取了屏幕截图。我用这种方式睡了一段时间

driver.execute_script("""arguments[0].scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' });""", element)
time.sleep(1)
scr = driver.get_screenshot_as_png()

但我真的不想使用睡眠,因为它们不是面向测试的。我试图等待元素可见,但它也不起作用。另一种尝试是使用 ActionChains 移动到元素,但它没有显示整个元素在其上移动。有没有办法等待滚动完成?除了屏幕截图的这种特定情况之外,了解等待滚动完成的方法会很有用。

标签: javascriptpythonselenium

解决方案


public static void fullScren(String filename) throws IOException
{

        Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
        ImageIO.write(myScreenshot.getImage(),"PNG",new File("src/main/java/ScreenShots/" + filename + ".jpg"));
    }

您必须使用此向下滚动并通过 selenium 进行全屏拍摄


推荐阅读