【问题标题】:How to press Page Down key in Selenium to scroll down a webpage through Java?如何在 Selenium 中按 Page Down 键通过 Java 向下滚动网页?
【发布时间】:2018-09-14 20:10:16
【问题描述】:

我想向下滚动我的网页。我应该在 Selenium 中使用什么 Java 命令?

【问题讨论】:

  • 您要任意向下滚动还是滚动到目标网页元素?
  • 请添加更多详细信息,为什么要滚动页面?出于什么原因?任何东西都可以通过 selenium 网络驱动程序在网页上定位。

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

向下滚动网页不是一个有效的用例,也许您想向下滚动以在Viewport 中引入一个 WebElement 以与之交互.为此,您可以使用executeScript() 方法,如下所示:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);

【讨论】:

    【解决方案2】:

    使用下面的代码试试,

            JavascriptExecutor js = (JavascriptExecutor) driver;
    
            // Launch the application       
            driver.get("http://demo.guru99.com/test/guru99home/");
    
            //This will scroll the web page till end.       
            js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
    

    【讨论】:

      【解决方案3】:

      请参考以下代码:

      1) 使用动作类****

      WebDriver driver = new ChromeDriver();
                  //Creating an object 'action' 
      Actions action = new Actions(driver);
                  //open SoftwareTestingMaterial.com
      driver.get(Site URL);   // Give site URL as name of the web page
                  //sleep for 3secs to load the page
      Thread.sleep(3000);
                  //SCROLL DOWN
      action.sendKeys(Keys.PAGE_DOWN).build().perform();
      Thread.sleep(3000);
                  //SCROLL UP
      action.sendKeys(Keys.PAGE_UP).build().perform();
      

      【讨论】:

        猜你喜欢
        • 2018-01-07
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 1970-01-01
        • 2017-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多