【问题标题】:Selenium: horizontal scroll using Actions classSelenium:使用 Actions 类的水平滚动
【发布时间】:2018-04-12 13:54:15
【问题描述】:

我尝试了各种方法来通过 Selenium 操作和 Javascript 执行器访问我的网页上的这个自定义滚动条。滚动条仅滚动 500 像素,而实际上我希望它滚动整个宽度。任何帮助表示赞赏。以下是我当前的代码 sn-p:

 WebElement slider = element("slider_div");
 WebElement scrollbar = element("scrollbar");
 int w = slider.getSize().getWidth();
 Actions move = new Actions(driver);
 move.dragAndDropBy(e, offset, 0).build() .perform();

我也尝试了以下解决方案,但仍然没有成功:

WebElement slider = element("slider_div");
WebElement scrollbar = element("scrollbar");
int w = slider.getSize().getWidth();
clickByJavascript(slider);
Actions action = new Actions(driver); 
action.sendKeys(Keys.RIGHT).build().perform();`

提前致谢!!!

【问题讨论】:

  • WebElement slider = element("slider_div");WebElement scrollbar = element("scrollbar"); 到底指的是什么?
  • 嗨 Debanjan,'slider' 指的是滚动 div,'scrollbar' 指的是滚动条元素。让我粘贴下面的 HTML:
  • 您能否提供更多与Html页面相关的信息,您的问题与slide element or scroll page|element有关
  • 我的问题与滚动页面|元素有关。它是一个 div 中的自定义滚动条
  • 你能分享任何示例页面来尝试元素水平滚动。你的问题对吗?

标签: selenium selenium-webdriver scrollbar action mcustomscrollbar


【解决方案1】:

我已经在下面指定的站点上测试了您的场景,包括水平和垂直滚动。

测试网址 « https://trello.com/b/LakLkQBW/jsfiddle-roadmap;

元素内垂直滚动 «

  • 硒java:

    WebElement element = driver.findElement(By.xpath("//div[@id='board']/div[1]/div[1]/div[2]") );
    for (int i = 0; i < 5; i++) {
        jse.executeScript("arguments[0].scrollTop += 200;", element);
    }
    
  • javascript|jquery:

    var objDiv = $x("//div[@id='board']/div[1]/div[1]/div[2]")[0];
    console.log( objDiv );
    
    objDiv.scrollTop += 100; 
    //objDiv.scrollTop = objDiv.scrollHeight;
    

元素内水平滚动 «

  • Java 动作类

    WebElement horizontalbar = driver.findElement(By.id("board") );
    Actions action = new Actions(driver);
    
    Actions moveToElement = action.moveToElement( horizontalbar );
    for (int i = 0; i < 5; i++) {
        moveToElement.sendKeys(Keys.RIGHT).build().perform();
    }
    

滚动直到元素出现。

  • javascript

    public void scroll_Till_Element(String id) {
        WebElement element = driver.findElement(By.id( id ) );
        jse.executeScript("arguments[0].scrollIntoView(true);", element);
    }
    

滚动到页面末尾。

public void scrollPage() throws InterruptedException {
    driver.get("https://stackoverflow.com/q/33094727/5081877");

    Actions actions = new Actions(driver);
    WebElement element = driver.findElement(By.xpath("//body") );
    Actions scrollDown = actions.moveToElement( element );
    scrollDown.keyDown(Keys.CONTROL).sendKeys(Keys.END).build().perform();
    //jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");

    Thread.sleep( 1000 * 4 );

    /*Actions scrollUP = actions.moveToElement( element );
    scrollUP.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).build().perform();*/
    jse.executeScript("window.scrollTo(0, -document.body.scrollHeight)");

    scroll_Till_Element( "answer-33142037" );
}

对于 javascript,您可以使用其中任何一种。

window.scrollBy(0,800);
window.scrollTo(0, -document.body.scrollHeight);
scroll(0, -document.body.scrollHeight);

@see

【讨论】:

    【解决方案2】:

    使用下面的线条从左到右滚动

    ((JavascriptExecutor) driver).executeScript("window.scrollBy(500000, 0)");
    

    要从右向左滚动,请使用以下行:

    ((JavascriptExecutor) driver).executeScript("window.scrollBy(-500000, 0)");
    

    这将完全向左或向右滚动,因为 (50000) 是覆盖页面的一个非常大的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多