Selenium 2.0b3 IE WebDriver,点击不触发 [英] Selenium 2.0b3 IE WebDriver, Click not firing

查看:22
本文介绍了Selenium 2.0b3 IE WebDriver,点击不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IE9中使用IE驱动时,有时Click方法只会选择一个按钮,不会做Click()的动作.请注意,这只是偶尔发生,所以我认为这不是代码的问题.在 Firefox4 中使用 Firefox 驱动程序没有问题.我也遇到了一个问题,偶尔也找不到元素,但只能在 IE 中找到,而不是在 Firefox 中.

When using the IE driver with IE9, occasionally the Click method will only select a button, it wont do the action of the Click(). Note this only happens occasionally, so i don't think it is the code that is the problem. Using the Firefox driver with Firefox4 has no problems. I am also having an issue where elements are not being found occasionally too, but only in IE again, not Firefox.

if (Driver.FindElement(By.Name("username")) == null) {
    //sometimes gets here in IE, never gets here in Firefox
}
Driver.FindElement(By.Name("username")).SendKeys(username);
Driver.FindElement(By.Name("surname")).SendKeys(surname);
Driver.FindElement(By.Name("firstname")).SendKeys(firstname);
string url = Driver.Url;
Driver.FindElement(By.Name("cmd")).Click();
if (Driver.Url == url) {
    //if the page didnt change, click the link again
    Driver.FindElement(By.Name("cmd")).Click();
}

我见过类似的问题 (http://stackoverflow.com/questions/4737205/selenium-webdriver-ie-button-issue),但我没有动态生成的 ID.

I have seen this similar questions (http://stackoverflow.com/questions/4737205/selenium-webdriver-ie-button-issue), but i do not have dynamicly generated ids.

推荐答案

当我尝试使用 .Click() 单击链接时,我在 Internet Explorer 8 上发现了同样的问题 - 即使我可以看到Selenium 点击链接.根据我的经验,如果浏览器没有焦点,那么初始点击将不起作用.

I have found the same thing on Internet Explorer 8 when attempting to click links using .Click() - even though I can see Selenium clicking on the link. From my experience it appears that if the browser does not have focus then the initial click doesn't work.

对此的解决方法是将 .Click() 发送到页面上的另一个元素,以便浏览器在尝试单击链接之前获得焦点,例如它的父母:

A workaround to this is to send a .Click() to another element on the page, so that the browser gets the focus, before attempting to click the link, e.g. it's parent:

Driver.FindElement(By.Id("Logout")).FindElement(By.XPath("..")).Click();
Driver.FindElement(By.Id("Logout")).Click();

这篇关于Selenium 2.0b3 IE WebDriver,点击不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆