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();
-
1Can you give us the stacktrace? Or the exact exception that is thrown?spg– spg2014年01月15日 14:06:02 +00:00Commented Jan 15, 2014 at 14:06
-
Here, the problem is invisibility of a web element. The problem is not to find specific web elementuser3157427– user31574272014年01月15日 14:07:51 +00:00Commented Jan 15, 2014 at 14:07
-
{OpenQA.Selenium.ElementNotVisibleException: element not visibleuser3157427– user31574272014年01月15日 14:16:14 +00:00Commented 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?Arran– Arran2014年01月15日 15:23:12 +00:00Commented Jan 15, 2014 at 15:23
8 Answers 8
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]")));
1 Comment
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());
3 Comments
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]"));
2 Comments
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.
Comments
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.
Comments
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/
1 Comment
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.
1 Comment
.ElementIsPresent
instead of .ElementIsVisible
, like the question asked had implied?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.
Comments
Explore related questions
See similar questions with these tags.