4

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();
Ripon Al Wasim
37.9k42 gold badges159 silver badges179 bronze badges
asked Jan 15, 2014 at 13:56
4
  • 1
    Can you give us the stacktrace? Or the exact exception that is thrown? Commented Jan 15, 2014 at 14:06
  • Here, the problem is invisibility of a web element. The problem is not to find specific web element Commented Jan 15, 2014 at 14:07
  • {OpenQA.Selenium.ElementNotVisibleException: element not visible Commented Jan 15, 2014 at 14:16
  • Well what needs to happen to make it visible? What would a user need to do to make that element visible and be able to use it? Commented Jan 15, 2014 at 15:23

8 Answers 8

11

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:

  1. The first will actually simulate the click

    ((IJavaScriptExecutor)wd).ExecuteScript("arguments[0].click();", wd.FindElement(By.XPath("//input[@value=2]")));
    
  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]")));
    
Andrea
12.5k17 gold badges71 silver badges76 bronze badges
answered Mar 31, 2015 at 16:30

1 Comment

This is one of the few answers I've seen that note passing the Selenium-found element in instead of using Javascript functions to find it (which gets ugly with XPath), so you get a +1!
2

Could it actually be invisible at the point of checking? i.e. you need to wait for it. You can use a WebDriverWait to wait until it becomes visible.

e.g.

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
wait.Until(w=>w.FindElements(By.XPath("//input[@value=2]")).Any());
answered Jan 15, 2014 at 14:07

3 Comments

I used Threa.Sleep(2000) to wait 2 seconds, but it hadn't. İs there any difference
Yes, WebDriverWait lets you wait until a condition occurs, e.g. the element becomes visible.
So have you looked at the page elements in the browser at the point that this line is executing?
1

You can try to make it visible through javascript then make it back invisible after click:

IWebElement element = Driver.FindElement(By.XPath("//input[@value=2]"))
((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].hidden = false;", element);
element.click
((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].hidden = true;", element);

wrap it in extension method and use when you need it

public static IWebElement ClickOnInvisibleElement(this IWebDriver Driver, By by)
{
 IWebElement element = Driver.FindElement(by))
 ((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].hidden = false;", element);
 element.click
 ((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].hidden = true;", element);
 return element;
}
Driver.ClickOnInvisibleElement(By.XPath("//input[@value=2]"));
answered Jan 16, 2014 at 8:36

2 Comments

Is this even valid Javascript?
oops sorry, its a typo, i write it fast, was in hurry, edited
1

I like @JustinHarvey answer very much - as usually element is not visible for a reason and more often than not one simply needs to way for it to become visible. His script didn't work for me, however when I slightly modified it did it.

 WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
 wait.Until(w => driver.FindElements(By.CssSelector("[type='submit']")).ToList().Any(o => o.Displayed));

Hope this saves you some time.

answered Mar 2, 2016 at 12:40

Comments

1

As you are locating elements by XPath,Id or Class,sometimes it takes more time to load new HTML page after previous action.In this case,you just have to put more time on Thread.Sleep(time); to escape from the exception. You may wait for the element going to appear using the code below(C#):

Thread.Sleep(10000);
WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(30));
IWebElement element= wait.Until<IWebElement>((d) =>
{
 return d.FindElement(By.Id("elementID"));
});
element.Click();

Another way to click elements using IJavaScriptExecutor (C#):

IWebDriver webDriver;
IWebEelement webElement = webDriver.findElement(By.XPath(Element xpath));
IJavaScriptExecutor executor = (IJavaScriptExecutor)webDriver;
executor.ExecuteScript("arguments[0].click();", webElement );

The code is almost the same in Java.

I hope it helps you.

answered Jun 23, 2016 at 6:57

Comments

0

Use java script executer to do the magic.

 ((JavascriptExecutor) webdriver).executeScript("document.getElementById('btn')[0].click

Read this for more details : http://www.anitpatel.net/2012/02/25/selenium-webdriver-how-to-click-on-a-hidden-link-or-menu/

answered Jan 15, 2014 at 13:59

1 Comment

There is two radio button I have to choose one of them and only difference between them is their value . Their name and type and the same ı preffereed to chose by using value
0

It seems like your webdriver attempts to click the radio button when it is not visible. This can vary depending how fast the page loads. I would recommend using webdriverWait to wait for the radio button to become visible. Your code would look something like this:

var wait = new WebDriverWait(wd, TimeSpan.FromMinutes(1));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[@value=2]")));
wd.FindElement(By.XPath("//input[@value=2]")).Click();

The first line initializes the wait variable. What this does is it uses your allows the wait variable to use your webdriver to wait for certain elements, and it sets a time limit for how long to wait for (I set the wait time to one minute). The next line actually waits for the radio button to appear by waiting for the xpath to become visible. Finally, it clicks on the button if it is visible. If the wait.until command exceeds the allotted wait time, an exception is thrown.

answered Jan 15, 2014 at 14:43

1 Comment

Don't you mean .ElementIsPresent instead of .ElementIsVisible, like the question asked had implied?
0

I think the problem is that you are moving to the next step quickly before getting answers for the previous step. I had the same problem when in:
1st step: Open a window
2nd step: Click on window button

My problem was that the JS didn't return the data quick enough so I tried to press the button before the window came up (so I got the "invisible" error)

To fix that just place Thread.sleep(1000) in your code.

Jony Adamit
3,44641 silver badges46 bronze badges
answered May 19, 2015 at 8:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.