Selenium:使用 Actions class 的水平滚动

Selenium: horizontal scroll using Actions class

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

 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();`

提前致谢!!!

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

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

Element Inner Vertical Scroll «

  • 硒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);
    }
    
  • java脚本|jquery:

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

Element Inner Horizontal Scroll «

  • Java 操作 Class

    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();
    }
    

Scroll till the Element comes into view.

  • java脚本

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

Scroll till end of the page.

public void scrollPage() throws InterruptedException {
    driver.get("");

    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" );
}

对于 java脚本,您可以使用其中任何一个。

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

@见

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

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

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

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

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