I'm new to Selenium and fairly new to Java. I'm a tester by trade, not a developer, so I'm trying to gradually convert my Selenium IDE scripts into something more robust and 'proper'. Unfortunately I'm struggling with a fairly basic task. As soon as the web page loads, I want to click a button. This works fine in Selenium IDE, but only if I set the speed to 'slow'.
I can get it to work using the following code, but ONLY if I include the thread.sleep line. I've read that this is not a good idea, so I'm trying to implement something smarter using webDriverWait.
public class mytestclass {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "";
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
@Test
public void test1() throws Exception {
driver.get(baseUrl + "file:///C:/_VM/testpage.html");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("button_X")));
//Thread.sleep(2000);
driver.findElement(By.id("button_X")).click();
}
}
So the fact that this is working when executing the thread.sleep line leads me to believe the wait.until line is returning true when the button is not actually ready. I've also tried to switch the ExpectedConditions to other identifiers such as visibilityOfElementLocated(By locator) and presenceOfElementLocated(By locator) but these act the same way too.
Am I doing something wrong here? Is there something else I can try?
I'm afraid I can't link to the website as it's work related. It does read in a lot of images and runs a lot of javascript on startup, so it does take a moment to get going, but that's why I want to add the wait logic in!
Thanks for any advice.