1

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.

pledgeX
  • 25
  • 1
  • 1
  • 3
  • 1
    I never mix implicit and explicit waits so I can't be 100% sure that mixing them is your problem but mixing the two types of wait is definitely something you don't want to do. See [this](http://stackoverflow.com/a/15174978/1906307) and [this](http://stackoverflow.com/q/20268396/1906307). (Note: even if the other questions are in other languages, it does not matter. Selenium broadly works the same across languages.) – Louis Oct 21 '14 at 16:11

1 Answers1

1

Does your site's startup involve jQuery? If it does, you could try this. I have found it helpful in my tests. It is in C#, but I'm sure you can convert it to java.

Wait until jQuery is ready:

IWait<IWebDriver> wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(WAITFORELEMENT_TIMEOUT));
wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
JoriO
  • 1,050
  • 6
  • 13
  • Thanks for the feedback. After a bit of googling, I managed to find a java based equivalent of your code which is as follows: `private void waitForJQuery(WebDriver driver) { (new WebDriverWait(driver, 10)).until(new ExpectedCondition() { public Boolean apply(WebDriver d) { JavascriptExecutor js = (JavascriptExecutor) d; return (Boolean) js.executeScript("return jQuery.active == 0"); } }); }` I haven't quite got my head around how this works yet (next on my list!), but it does work. – pledgeX Oct 30 '14 at 16:44
  • This worked really well for tables cells that update via jQuery and finally don't have to use a Sleep.Timeout. Thank you! – firecape Mar 10 '18 at 23:10