0

The issue I'm currently having is, that I need a way to stop the driver for a bit. What I'm doing is placing input into a webpage, pressing a button, and grabbing the table that appears as a result. I got StaleElementReferenceException, making me think that perhaps the table was being scraped before it was fully loaded. Since implicit waits only wait until a certain element is present, I wasn't sure how to wait until the entire table was made. I tried Thread.sleep(), and it does seem to work, but it freezes the browser, and it does not seem like the right way to do things. So then I tried to use wait.until(ExpectedConditions.visibilityOfAllElements()), but I still get StaleElementReferenceException. Is there something I'm missing here?

Below is the code:

public void updateWinningNumbers(){

    try {

        driver = new FirefoxDriver();

        driver.navigate().to("http://nylottery.ny.gov/wps/portal/Home/Lottery/home/your+lottery/winning+numbers/win4pastwinning+numbers");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        driver.switchTo().frame("Winners_NumberTracker");

        Select startYear = new Select(driver.findElement(By.id("lottoStartDateYear")));
        Select startMonth = new Select(driver.findElement(By.id("lottoStartDateMonth")));
        Select endYear = new Select(driver.findElement(By.id("lottoEndDateYear")));
        Select endMonth = new Select(driver.findElement(By.id("lottoEndDateMonth")));

        WebElement enterButton = driver.findElement(By.name("getWinningNumbers"));

        Calendar c = Calendar.getInstance();
        int currYear = c.get(c.YEAR);
        System.out.println("CURRYEAR: " + currYear);
        int monthNum = c.get(c.MONTH);

        startYear.selectByValue("2008");
        startMonth.selectByIndex(0);
        endYear.selectByValue(String.valueOf(currYear));
        endMonth.selectByIndex(monthNum);

        enterButton.click();

        //THIS IS WHERE THINGS ARE ODD; TRY TO COMMENT OUT THIS TRY BLOCK AND SEE THE EXCEPTION THAT HAPPENS
//          try {
//              Thread.sleep(5000);
//          } catch (InterruptedException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }

        WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfAllElements(driver.findElements(By.cssSelector("#winningNumbersTable tbody td"))));


        List<WebElement> tableRows = driver.findElements(By.cssSelector("#winningNumbersTable tbody td"));
        PrintWriter writer = new PrintWriter("data/winners.txt");

        for(WebElement we : tableRows){
            writer.println(we.getText());
            writer.flush();
        }

        writer.close();
        driver.close();
        System.out.println("FINISHED!");

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

As a side-question, I was also wondering why switching to particular frames is necessary; it's something I added after Selenium insisted it couldn't find the elements I was looking for, although it seems that it should ultimately be in the same HTML file. Does it just require it to be in the particular frame for efficiency?

Thanks again, and apologize for the length!

Luiserebii
  • 55
  • 2
  • 11
  • please provide exception to know exactly where issue is in provided code – murali selenium May 06 '16 at 04:37
  • Sorry, I thought it was self-evident from what I wrote. The exception occurs at: List tableRows = driver.findElements(By.cssSelector("#winningNumbersTable tbody td")); and, if wait(until) is uncommented, it occurs at that line first. – Luiserebii May 06 '16 at 06:02
  • So, the website I was using has changed, and instead of loading the entire table in one page, it now loads it into seperate pages. The code is therefore different; I have no idea why it wasn't working before, but Thread.sleep() seems to work now. I'm posting the working code at this pastebin: http://pastebin.com/bKpivLss – Luiserebii May 13 '16 at 01:20

2 Answers2

0

You can try to use Fluent wait command ,

Wait wait = new FluentWait(driver)

.withTimeout(30, SECONDS)

.pollingEvery(5, SECONDS)

.ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function() {

public WebElement apply(WebDriver driver) {

return driver.findElement(By.id("foo"));

}

}); This will poll after every 5 secs and check if the element is present or not.

  • err, is that code working for you in the context of this problem? I'm not even sure some of that is Java syntax... Sorry, but I couldn't get this to work – Luiserebii May 09 '16 at 17:36
0

Firstly, query related to frames

In a very simple word, Frames or IFrames can be treated as web page inside a web page. And no wonder, Selenium also treats them in same line. That means, you have to switch to a frame, to find elements present inside that frame. And you need to come out of the frame when you want to find elements outside of the frame.

As per question and as well as comment said the StaleElementReferenceException is occurring at List, generally this issue occurs when reference to element is lost. In my experience driver generally wait for some time to load page when navigate to any URL or click on login which leads to loading page.

So as said

Thread.sleep not working expected

Wait for all elements also not working as expected

may we try waiting for specific element which loads lastly or last element in list

taking javascript executor help to wait until document state is complete. This link helps you on this.

Thank You, Murali

Community
  • 1
  • 1
murali selenium
  • 3,847
  • 2
  • 11
  • 20
  • This looks like an interesting solution... what I'm a little concerned about is the reliability, due to George's comment: "FYI - Even still the above does not guarentee that the page is complete - just that the dom is ready. Any dojo/jquery might still be dynamically building elements on the page so you might need first wait for dynamic elements before interacting with them." I'll go test this, though! – Luiserebii May 09 '16 at 17:38
  • Sorry, no dice. Anything else I can try? – Luiserebii May 13 '16 at 00:29