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条回答
倾城 Initia
2楼-- · 2019-02-01 03:25

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);
查看更多
姐就是有狂的资本
3楼-- · 2019-02-01 03:26

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

查看更多
爷的心禁止访问
4楼-- · 2019-02-01 03:27

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.

查看更多
唯我独甜
5楼-- · 2019-02-01 03:28

Try this:

selenium.fireEvent(ID, "keypress");
查看更多
对你真心纯属浪费
6楼-- · 2019-02-01 03:30

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

查看更多
祖国的老花朵
7楼-- · 2019-02-01 03:31

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)

查看更多
登录 后发表回答