Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium 2.0b3 IE WebDriver, Click not firing

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.

like image 750
djeeg Avatar asked Apr 07 '11 01:04

djeeg


People also ask

Why click is not working in Selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

How do I use Selenium to click a website?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

What is click command in Selenium?

To put it in simple words, the click command emulates a click operation for a link, button, checkbox or radio button. In Selenium Webdriver, execute click after finding an element. In SeleniumIDE, the recorder will do the identifying, and the command is simply click.

What are the ways to click in Selenium?

We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.


2 Answers

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(); 
like image 149
Naishy Avatar answered Sep 18 '22 21:09

Naishy


I find the IE driver buggy and the same version of my code behaves differently on different machines with the same version of IE.

To get consistently before each action I do the following.

   driver.SwitchTo().Window(driver.CurrentWindowHandle);//Force Focus 

For me this makes the IE driver behave more as expected.

like image 36
Simon Avatar answered Sep 21 '22 21:09

Simon