硒:使用Actions类进行水平滚动 [英] Selenium: horizontal scroll using Actions class

查看:140
本文介绍了硒:使用Actions类进行水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我也尝试了以下解决方案,但仍然无法解决问题:

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

在此先感谢!!!

解决方案

我已经在下面指定的站点上使用水平和垂直滚动条测试了您的方案。 / p>

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


元素内部垂直滚动«





  • 硒java:

      WebElement元素= 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;,元素);
    }




  • 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 Actions类

      WebElement horizo​​ntalbar = driver.findElement(By.id( board )); 
    Actions action = new Actions(driver);

    动作moveToElement = action.moveToElement(horizo​​ntalbar);
    for(int i = 0; i< 5; i ++){
    moveToElement.sendKeys(Keys.RIGHT).build()。perform();
    }







滚动直到元素出现。





  • javascript

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







滚动到页面结尾。






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

动作=新的动作(驱动程序);
WebElement元素= driver.findElement(By.xpath( // body));
动作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);

/ *动作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




I have tried various to access this custom scroll bar on my web page through Selenium actions as well as Javascript Executor. The scroll bar scrolls through only 500 pixels when in fact I want it to scroll throughout the complete width. Any help is appreciated. Below is my current code snippet:

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

I have also tried with the below solution, but still it did not work out:

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

Thanks in Advance!!!

解决方案

I have tested your scenarion over the site specified below, with horizontal and vertical scrolls.

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

Element Inner Vertical Scroll «

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

Element Inner Horizontal Scroll «

  • Java Actions 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.

  • javascript

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

for javascript you can use any of these.

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

@see

这篇关于硒:使用Actions类进行水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆