当将IE驱动程序与IE9一起使用时,Click方法有时只会选择一个按钮,而不会执行Click()的操作。请注意,这只是偶尔发生,所以我不认为这是问题所在的代码。在Firefox4中使用Firefox驱动程序没有问题。我也遇到一个问题,即有时也不会在IE中找到元素,而只是在IE中而不是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。
最佳答案
尝试使用.Click()
单击链接时,我在Internet Explorer 8上发现了相同的内容-即使我看到Selenium也单击了链接。根据我的经验,如果浏览器没有焦点,则初始单击将不起作用。
一种解决方法是将.Click()
发送到页面上的另一个元素,以使浏览器在尝试单击链接之前获得焦点,例如是 parent :
Driver.FindElement(By.Id("Logout")).FindElement(By.XPath("..")).Click();
Driver.FindElement(By.Id("Logout")).Click();
关于internet-explorer-9 - Selenium 2.0b3 IE WebDriver,单击不触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5574802/