Java Selenium click() 事件似乎并不总是被触发 => 导致超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/633600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium click() event seems not to be always triggered => results in timeout?
提问by blackicecube
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.
链接“mylink”确实存在,click() 的第一次调用始终有效。但是第二次 click() 有时似乎有效,有时则无效。
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.
看起来 click() 事件根本没有触发,因为页面甚至没有开始加载。不幸的是,这种行为是不够确定的。
Here's what I already tried:
这是我已经尝试过的:
Set longer time timeout
=> did not helpWait 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:
现在我最终调用了 click() 两次,所以:
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':
这会起作用,但这不是一个很好的解决方案。我还在另一个论坛上看到有人建议写一些类似“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?
但我认为这也不是一个合适的解决方案......任何想法/解释为什么有时不触发 click() 事件?
回答by Vijay Kotari
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.
此外,请等待链接出现并可见,然后再单击它。
回答by eglasius
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);
回答by Eric Wendelin
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.
我已经使用 selenium 一段时间了,我真的不喜欢 waitForPageToLoad()。您可能会考虑始终等待相关元素存在。
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.
我发现这种方法似乎可以解决我遇到的最奇怪的问题。另一种可能性是您可能有一些 javascript 阻止链接在第一次单击时执行任何操作。这似乎不太可能,但值得仔细检查。
回答by Andrew Martinez
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 只是不喜欢点击某些锚标签。我不确定是什么原因造成的,但它发生了。我发现在这些情况下有一个麻烦的链接而不是做
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:...." >
回答by Firefight
I just tried WebDriver (Selenium 2.0) and found that WebElement#sendKeys(Keys.ENTER) works.
我刚刚尝试了 WebDriver (Selenium 2.0) 并发现 WebElement#sendKeys(Keys.ENTER) 有效。
回答by Amine
Try this:
尝试这个:
selenium.fireEvent(ID, "keypress");
回答by Mayank
selenium.click("link=Continue to this website (not recommended).");
Thread.sleep(5000);
回答by Vinesh
Here this one will work:
在这里,这个将起作用:
selenium.waitForPageToLoad("60000");
selenium.click("link= my link");
回答by Jesse Sanders
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:
C#代码:
var text = Selenium.GetText(myLocator);
Selenium.Click("link=" + text);
回答by sakhunzai
I am having the same issue :( with selenium IDE 1.0.10 , phpunit 3.5 , selenium RC server 1.0.3
我遇到了同样的问题 :( 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)