簡體   English   中英

Selenium:使用Actions類的水平滾動

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

提前致謝!!!

我已經在下面指定的網站上測試了你的情節,水平和垂直滾動。

測試網址« https://trello.com/b/LakLkQBW/jsfiddle-roadmap ;

元素內部垂直滾動«

  • Selenium 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);

@看到

使用以下線條從左向右滾動

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

要從右向左滾動,請使用以下行:

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

這將完全向左或向右滾動,因為(50000)是覆蓋頁面的非常大的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM