Selenium click() event seems not to be always trig

2019-02-01 02:43发布

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?

14条回答
叼着烟拽天下
2楼-- · 2019-02-01 03:15

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.

查看更多
疯言疯语
3楼-- · 2019-02-01 03:17

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

查看更多
你好瞎i
4楼-- · 2019-02-01 03:21

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)
查看更多
Juvenile、少年°
5楼-- · 2019-02-01 03:22

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:...." >
查看更多
Rolldiameter
6楼-- · 2019-02-01 03:22

Here this one will work:

selenium.waitForPageToLoad("60000");

selenium.click("link= my link");
查看更多
我命由我不由天
7楼-- · 2019-02-01 03:22

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);
查看更多
登录 后发表回答