Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium click() event seems not to be always triggered => results in timeout?

Here's what I do:

selenium.click("link=mylink"); selenium.waitForPageToLoad(60000);  // do something, then navigate to a different page  // (window focus is never changed in-between)  selenium.click("link=mylink"); selenium.waitForPageToLoad(60000); 

The link "mylink" does exist, the first invocation of click() always works. But the second click() sometimes seems to work, sometimes not.

It looks like the click() event is not triggered at all, because the page doesn't even start to load. Unfortunately this behaviour is underterministic.

Here's what I already tried:

  1. Set longer time timeout
    => did not help

  2. Wait for an element present after loading one page
    => doesn't work either since the page does not even start to load

For now I ended up invoking click() twice, so:

selenium.click("link=mylink"); selenium.waitForPageToLoad(60000);  // do something, then navigate to a different page  // (window focus is never changed in-between)  selenium.click("link=mylink"); selenium.click("link=mylink"); selenium.waitForPageToLoad(60000); 

That will work, but it's not a really nice solution. I've also seen in another forum where someone suggested to write something like a 'clickAndWaitWithRetry':

  try {       super.click("link=mylink");       super.waitForPageToLoad(60000);   }   catch (SeleniumException e) {       super.click("link=mylink");       super.waitForPageToLoad(60000);   } 

But I think that is also not a proper solution.... Any ideas/explanations why the click() event is sometimes not triggered?

like image 285
blackicecube Avatar asked Mar 11 '09 07:03

blackicecube


People also ask

Why click is not working in selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

How would you click on a button on which WebElement click doesn't work?

1 Answer. Try clicking the element through Action class. WebElement webElement = driver. findElement(By.id("Your ID Here"));

What are the different ways to click on any element apart from Click () method in selenium?

Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.


2 Answers

Sometimes, seemingly randomly, Selenium just doesn't like to click certain anchor tags. I am not sure what causes it, but it happens. I find in those cases w/ a troublesome link instead of doing

selenium.click(...) 

do

selenium.fireEvent( locator, 'click' ); 

As others have stated above me, I have specifically had issues with anchor tags that appear as follows:

<a href="javascript:...." > 
like image 56
Andrew Martinez Avatar answered Sep 20 '22 14:09

Andrew Martinez


I've done selenium for awhile, and I really have developed a dislike for waitForPageToLoad(). You might consider always just waiting for the element in question to exist.

I find that this method seems to resolve most weird issues I run into like this. The other possibility is that you may have some javascript preventing the link from doing anything when clicked the first time. It seems unlikely but worth a double-check.

like image 21
Eric Wendelin Avatar answered Sep 21 '22 14:09

Eric Wendelin