2013-04-22 88 views
11

我想使用此代码滚动页面向下滚动我的网页和IM它不工作如何,但向下滚动使用硒的webdriver与Java

public ViewBasketSentToMePageObject viewSlideShare() throws InterruptedException { 

     Thread.sleep(500l); 


     Actions action1 =new Actions(getDriver()); 
     action1.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0030')).build().perform(); 

     List<WebElement> function = getDriver().findElements(By.xpath("//a [@ng-click='onItemClick()']")); 
     function.get(13).findElement(By.xpath("//img [@ng-src='resources/images/slideshare-icon-small.png']")).click(); 

     return getFactory().create(ViewBasketSentToMePageObject.class); 
    } 

寻找帮助

+2

您的问题早先在这里解决:http://stackoverflow.com/questions/12293158/page-scroll-up-or-down-in-webdriver-selenium-2-using-java – maximkou 2013-04-22 06:12:13

回答

19

尝试使用简单下面的java脚本,你可以滚动页面。

JavascriptExecutor jsx = (JavascriptExecutor)driver; 
jsx.executeScript("window.scrollBy(0,450)", ""); 
6

对于向下滚动:

WebDriver driver = new FirefoxDriver(); 
JavascriptExecutor jse = (JavascriptExecutor)driver; 
jse.executeScript("scroll(0, 250);"); 

或者,你可以做如下:

jse.executeScript("window.scrollBy(0,250)", ""); 
0

滚动,直到找到WebElement

试试这个:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement); 
0
WebElement element = 

driver.findElement(By.xpath("//input [@id='giveid']")); 

((JavascriptExecutor) 

driver).executeScript("arguments[0].scrollIntoView();". element); 

使用这个。这将帮助您向下滚动特定元素。我甚至在我的网站上进行过测试。它工作正常。有关更多信息,请参阅我的网站imo for pc