-2

I have below piece of code, when executed it gets to correct page what I expect and clicks the needed links. But, the result is always shows Fail instead of Pass. After last step in the for loop, the focus jumps to catch loop and prints the resultDetails.setFlag(false) and case fails. Please let me know where I'm wrong.

driver.findElement(By.linkText("ALL EQUIPMENT")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

List <WebElement> listings = driver.findElements(By.cssSelector("a[href*='/listing?listingid']"));

try {
    for (int i=0; i < listings.size(); i++) {
        WebElement requiredlisting = listings.get(i);
        requiredlisting.click();    

        getvalue = driver.findElement(By.xpath("//div[7]/span")).getText();
        System.out.println(getvalue);


        driver.findElement(By.xpath("//div[3]/div[2]/input")).click();
        Thread.sleep(10000);
        driver.findElement(By.id("listingQuestion")).click();
        Thread.sleep(10000);
        driver.findElement(By.id("listingQuestion")).sendKeys("Where is the listing located");
        Thread.sleep(10000);             
        driver.findElement(By.name("submitq")).click();
        Thread.sleep(10000);                         
        driver.findElement(By.xpath("//div/div[2]/div[3]/input")).click();
        Thread.sleep(10000);

        driver.findElement(By.id("uname")).click();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.linkText("Sign Out")).click();

        driver.manage().deleteAllCookies();
        driver.navigate().refresh();

        driver.get(field);
        driver.findElement(By.id("aurid")).sendKeys("mglaz@assetnation.com");

        driver.findElement(By.id("apwd")).sendKeys("Equipment1$");
        driver.findElement(By.xpath("//input[@value='Login']")).click();

        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.findElement(By.id("userAgreementBtn")).click();

        Thread.sleep(10000);

        System.out.println(getvalue);
        driver.findElement(By.id("quicksearch")).sendKeys(getvalue);
        Thread.sleep(10000);
        driver.findElement(By.cssSelector("input.quicksearch_go")).click();

        Thread.sleep(20000);

        driver.findElement(By.linkText("Lot Q/A")).click();
        Thread.sleep(10000);

        driver.getPageSource().contains("my question");
        driver.manage().deleteAllCookies();
        driver.navigate().refresh();
        resultDetails.setFlag(true);
    } 
}
catch (Exception e ) {
    resultDetails.setFlag(false);
}
Richard
  • 8,961
  • 3
  • 38
  • 47
Aditya
  • 457
  • 2
  • 8
  • 27
  • Did you try (int i=0; i < listings.size()-1; i++) – Helping Hands May 01 '15 at 04:52
  • 2
    You should log the exception to the console. That should tell you what line is throwing the exception, and should also give you an idea of why it is throwing an exception. – Richard May 01 '15 at 04:53
  • I have tried changing the For loop, but still seeing same issue. – Aditya May 01 '15 at 05:13
  • What exception error you getting , Try to print actual error from catch by e.printStackTrace(); – Helping Hands May 01 '15 at 06:17
  • org.openqa.selenium.StaleElementReferenceException: stale element reference: ele ment is not attached to the page document (Session info: chrome=42.0.2311.135) (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86) (WARNIN G: The server did not provide any stacktrace information) Command duration or timeout: 75 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/ stale_element_reference.html is the error what I see from Catch block – Aditya May 01 '15 at 08:39
  • 1
    You did not provide enough information; see https://stackoverflow.com/help/mcve to help you next time. You are doing all sorts of `.click()`s and so I am guessing some of them are navigating away from the original page. This will cause your `listings` to become stale. Have a look here http://stackoverflow.com/a/24785756/3124333 for possible help. – SiKing May 01 '15 at 14:42

1 Answers1

0

You are holding webelemets in 'listings'. By using for loop, you are taken one element and done lot of actions so the page is changed. if you call another webelement from listings which usually loses it's focus on page as it is changed, you are getting that stale_element_exception..

i hope you need to check/change logic over here,if i am not wrong....

Thank You, Murali

murali selenium
  • 3,847
  • 2
  • 11
  • 20