views:

6689

answers:

9

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?

A: 

The page has not loaded properly when you are clicking on it. Check for different elements on the page to be sure that the page has loaded.

Also, wait for the link to appear and be visible before you click on it.

Vijay Kotari
+1  A: 

Make sure you are increasing the timeout in the correct place. The lines you posted are:

selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);

This wait is for the page to load that comes back After the click. But the problem you describe is that it is failing when trying to do the click. So, make sure to increase the wait Before this one.

selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);

// do something, then navigate to a different page 
// (window focus is never changed in-between)
// after the last click in these steps:
selenium.waitForPageToLoad(60000);
// anything else that happened after that

selenium.click("link=mylink");
selenium.waitForPageToLoad(60000);
eglasius
+3  A: 

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.

Eric Wendelin
The page which shows the link is completely loaded (waitForElement was successful).It's just a normal html link (<a href="...">) no JS interaction. The link is in the navigation menu and as I wrote before, click works the 1st time but the click() event seems not to be triggered the 2nd time.
blackicecube
Hey, even I am facing an issue with javascript hyperlinks. I have a button with href="javascript:;". When I click on it using selenium-rc, the browser's status bar shows "javascript:;", but, no action takes place. Any workarounds for this??? - Vamyip
vamyip
The first thing I try when that happens is use mouseDown and mouseUp instead of click. That usually does the trick, even if it's kinda dumb.
Eric Wendelin
+4  A: 

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:...." >
Raegx
Hi, I could not make it work, it still says it have worked, but it does nothing!. The click command works but keeps waiting for the page to load (when it shouldn't). Any suggestion?
Diego Jancic
I have the same problem as Diego... Any suggestions?
Ekaterina
A: 

How can I "click" a javascript link using the Selenium IDE? ie, my href starts "javascript:...". Command: fireEvent Target: link=Expand Value: click (doesn't work) thx JB

PS: to clarify, the CLICK action does work, but then it "waits" until timeout (even though I'm using the click vs. clickAndWait fcn).

Jonathan
I used the beatnicClick technique described elsewhere to successfully click javascript-based static links. The problem I have now is that I have repeaters that dynamically create rows with javascript-based links, so I can't assign a known (static) ID to those anchor tags because they are created dynamically. WHY CAN'T SELENIUM HANDLE JAVASCRIPT LINKS???? argh
Jonathan
A: 

I just tried WebDriver (Selenium 2.0) and found that WebElement#sendKeys(Keys.ENTER) works.

Firefight
A: 

I am still not able to fire click event of anchor. what to do

Alok Kumar
A: 

Here try this : selenium.fireEvent(ID, "keypress");

Amine
A: 

selenium.click("link=Continue to this website (not recommended)."); Thread.sleep(5000);

Mayank