简体   繁体   English

使用带有Java的Selenium Webdriver滚动Webelement

[英]Scrolling Webelement using Selenium Webdriver with Java

Please help me to handle StaleElementReferanceException. 请帮助我处理StaleElementReferanceException。 I am trying to scroll a webelement using action class. 我正在尝试使用操作类滚动Web元素。 Below is the code: 下面是代码:

public void newsSearch() throws StaleElementReferenceException{

    driver.get("https://pi.persistent.co.in");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.findElement(By.id("ui-accordion-1-header-2")).click();

    Actions act = new Actions(driver);
    int numOfPixels = 2;
    WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("//*[@id='ui-accordion-1-panel-2']/div/div[2]/div[2]/div"));

    for (int i = 2; i < 50; i = i + numOfPixels) {

        act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().perform();
        System.out.println("Inside for loop");

    }
    System.out.println("After for loop");
    driver.findElement(By.xpath("//*[@id='ulNotification']/li[5]/h3")).click();
    driver.close();
}

It is giving an Exception: 它给出了一个异常:

org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 30.16 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'BHD22524', ip: '10.11.66.6', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_55'
Session ID: 03f8c7ca-a582-41b5-80d3-2ec53de719f5
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=37.0.1}]

At line number 50 ie 在第50行,即

act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().perform();

I tried using try catch, still it is not working. 我尝试使用try catch,但仍然无法正常工作。 Thank you in advance. 先感谢您。

In for loop condition at number of iteration you are getting exception? 在for循环条件下,迭代次数越来越多?

Because once the element is moved or dragged, page DOM will update and that element will not longer available to move again. 因为一旦元素被移动或拖动,页面DOM将更新,并且该元素将不再可用于再次移动。 That's the reason your are getting StaleElementReferenceException . 这就是您获取StaleElementReferenceException的原因。 See this explanation about StaleElementReferenceException 请参阅有关StaleElementReferenceException的说明

And your code to move element should be like below: 您要移动元素的代码应如下所示:

act.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0, numOfPixels).release().build().perform();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM