How to click an element which is invisible in Selenium WebDriver?

.net, c#, selenium, selenium-webdriver

Solution

Using javascript is a good option when wanting to click on hidden elements. Selenium CANNOT perform actions on hidden elements (ie clicking). You have two options for javascript functions:

The first will actually simulate the click

((IJavaScriptExecutor)wd).ExecuteScript("arguments[0].click();", wd.FindElement(By.XPath("//input[@value=2]")));

The second will simply trigger the event that is supposed to happen when the click occurs.

((IJavaScriptExecutor)wd).ExecuteScript("arguments[0].trigger('click');", wd.FindElement(By.XPath("//input[@value=2]")));

Problem

I want to click a radio button but ı sometimes get the exception "invisible element". I used Thread.Sleep() function but had not been. It occurs sometimes not always. I usually can click the radio button by using selenium web driver ``` wd.FindElement(By.XPath("//input[@value=2]")).Click(); ```

Original source