Selenium click() event seems not to be always trig

2019-02-01 02:45发布

问题:

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?

回答1:

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:...." >


回答2:

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.



回答3:

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



回答4:

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

Try selenium.pause before Selenium.click command. I have tried all above but none of them seems to resolve our problem. So finally we got a Magic selenium.pause which solved problem for me..

Hope this will solve your problem as well



回答5:

I am running into this issue now also. From my usages of this, it seems like the following is the most consistent:

@browser.click(selector, {:wait_for => :page})

Not exactly sure why that would be. But it seems that if you do:

@browser.click(selector)
   [maybe some stuff here too]
@browser.wait_for(:wait_for => :page)

Then you could end up waiting for a page that has already been loaded (i.e. you end up waiting forever).

I dug into the Selenium source code and found this nugget:

  def click(locator, options={})
    remote_control_command "click", [locator,]
    wait_for options
  end

...

  # Waits for a new page to load.
  #
  # Selenium constantly keeps track of new pages loading, and sets a
  # "newPageLoaded" flag when it first notices a page load. Running
  # any other Selenium command after turns the flag to false. Hence,
  # if you want to wait for a page to load, you must wait immediately
  # after a Selenium command that caused a page-load.
  #
  # * 'timeout_in_seconds' is a timeout in seconds, after which this
  #   command will return with an error
  def wait_for_page(timeout_in_seconds=nil)
    remote_control_command "waitForPageToLoad",
        [actual_timeout_in_milliseconds(timeout_in_seconds),]
  end
  alias_method :wait_for_page_to_load, :wait_for_page

Basically, this is doing the following:

@browser.click(selector)
@browser.wait_for(:wait_for => :page)

However, as the comment states, the first thing necessary is to use the :wait_for command immediately after.

And of course... switching the order puts you into the same wait forever state.

@browser.wait_for(:wait_for => :page)
@browser.click(selector)

Without knowing all the details of Selenium, it seems as though Selenium needs to register the :wait_for trigger when it is passed as an option with click. Otherwise, you could end up waiting forever if you somehow tell Selenium to wait the very instant before :wait_for is called.



回答6:

Here this one will work:

selenium.waitForPageToLoad("60000");

selenium.click("link= my link");


回答7:

I had the same problem - with Selenium 1.0.12 and Firefox 5.0 ; I managed to make the automated tests work this way:

  • I removed all "AndWait" commands (sometime they hang the test/browser)
  • I added a pause before the click
  • I added a waitForVisible after the click (usually I wait for the next html control I want to interact with on next page).

It goes like this:

  • waitForVisible OK
  • pause 1000
  • click OK
  • waitForVisible link=Go
  • pause 1000
  • click Go

etc...

It seems that the "waitForVisible" is triggered too soon, i.e. before the event handler are plugged into the control (thus clicking on the control has no effect). If you wait for 1 second, it's enought to plug/activate the click handlers...



回答8:

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.



回答9:

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);


回答10:

If you're using FireFox, make sure you're using 3.6 or later. WaitForPageToLoad uses the javascript variable 'readyState', but Firefox only supported this in 3.6. Earlier versions just don't wait

(see org.openqa.selenium.internal.seleniumemulation.WaitForPageToLoad)



回答11:

I am having the same issue :( with selenium IDE 1.0.10 , phpunit 3.5 , selenium RC server 1.0.3

 EDITED: 
 The culprit seems to be browser FF 3.6.13 , after upgrade to FF 3.6.14
  all my errors are  gone . My tests are working like charm :). 

 Selenium IDE 1.0.10
 PHPUnit: 3.5.6
 Selenium Server:selenium-2.0b1 (selenium-2.0b2 is buggy)


回答12:

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


回答13:

I've been having the same issue and found that I have to get the text of the link first. I know it's not the ideal way to do it, but I'm fortunate my links are uniquely named.

C# code:

var text = Selenium.GetText(myLocator);
Selenium.Click("link=" + text);


回答14:

Try this:

selenium.fireEvent(ID, "keypress");