Selenium 2.0b3 IE WebDriver, Click not firing

djeeg picture djeeg · Apr 7, 2011 · Viewed 49k times · Source

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();
}

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

Answer

Naishy picture Naishy · Apr 7, 2011

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.

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();