I'm developing a program to automate some forms filling and I'm using Selenium. A problem that has been bugging me since the start if this work, is that an instruction that was supposed to click a button does not work and returns no error. Tis happens randomly, but it used to happen more frequently on the first time I run the program.
Basically the driver must enter a site, fill a popup window with the login and then when the first page loads, has to click a button, which will open a second page. What happens is that without opening the second page, the driver is already looking for an element that belongs to it.
At first I thought that the brower and the driver could be unsynchronized with the driver being faster than the browser and somehow messing up the whole process. I tried to correct this with a sleep of half a second. Also implemented an implicit waiting of 15 seconds.
Without any improvements, I decided to check if the first element is found. I've inserted a line to print the text on the button to the console and it is printed correctly. The next line would be to click that button, and although the driver retrieves no errors, on the browser, only the first page is loaded.
Last but not the least tried to find all elements with that xpath, but the list has only one element.
So, to sum up, the first element is found, the clicking instruction retrives no errors but still the button is not clicked. This happens randomly.
Here's the relevant part of the code I'm using:
tester.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
tester.get(website);
Thread.sleep(1000);
//Bypasses login pop-up window
try
{
bypassLogin(username, password);
}
catch(Exception e)
{
handler(e.getMessage(), null);
finisher(tester, br, br2, pathGecko, pathTemplate);
}
//Sleep in order to synchronize driver and browser
Thread.sleep(1000);
while((reader = br2.readLine()) != null)
{
tempOrig = new StringBuilder()
.append(tempOrig)
.append(reader)
.toString();
}
//Enters the body frame, with the elements to work with
try
{
tester.switchTo().frame("portal_header");
List <WebElement> LClick = tester.findElements(By.xpath(".//*[@id='2']/a"));
System.out.println(LClick.size()); //Returns 1
tester.findElement(By.xpath(".//*[@id='2']/a")).getText(); //Returns the correct value
tester.findElement(By.xpath(".//*[@id='2']/a")).click();
tester.switchTo().defaultContent();
tester.switchTo().frame("portal_body");
}
catch(Exception e)
{
handler(e.getMessage(), null);
finisher(tester, br, br2, pathGecko, pathTemplate);
}