【问题标题】:How to scroll down an element in Selenium java如何在 Selenium java 中向下滚动元素
【发布时间】:2021-03-23 17:09:58
【问题描述】:

我有一个排行榜元素,我需要向下滚动才能使用 Selenium Java 在网页顶部查看所有玩家。在过去的 2 小时谷歌搜索之后,我只能通过向下滚动一个我不需要的页面来找到答案。

我已经尝试了以下

        //click on leaderboard
    driver.findElement(By.xpath("//*[@id=\"root\"]/div/div/div[2]/div[1]/div[2]/div[1]/div/div")).click();

    //scroll
    WebElement scroll = driver.findElement(By.xpath("//*[@id=\"accordion-panel-1720\"]/div[2]/div"));
    Actions actions = new Actions(driver);
    actions.moveToElement(scroll).perform();

        WebElement scroll = driver.findElement(By.xpath("//*[@id=\"accordion-panel-1720\"]/div[2]/div"));
    Actions act = new Actions(scroll);
    act.sendKeys(Keys.PAGE_DOWN).build().perform(); //Page Down
    System.out.println("Scroll down perfomed");

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    到目前为止,我已经找到了两种方法可以做到这一点: 使用 Keys.ARROW_DOWN、Keys.PAGE_DOWN 或执行 Javascript 来向上/向下滚动元素。

    JavascriptExecutor jsExec = (JavascriptExecutor) driver;
    jsExec.executeScript("document.getElementById('id').scrollTop += 100");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2017-01-03
      • 2018-06-08
      相关资源
      最近更新 更多