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!