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

21,737

Solution 1

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]")));
    

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

Solution 3

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.

Solution 4

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]"));

Solution 5

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.

Share:
21,737
user3157427
Author by

user3157427

Updated on June 23, 2020

Comments

  • user3157427
    user3157427 almost 4 years

    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();
    
    • spg
      spg over 10 years
      Can you give us the stacktrace? Or the exact exception that is thrown?
    • user3157427
      user3157427 over 10 years
      Here, the problem is invisibility of a web element. The problem is not to find specific web element
    • user3157427
      user3157427 over 10 years
      {OpenQA.Selenium.ElementNotVisibleException: element not visible
    • Arran
      Arran over 10 years
      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?
  • user3157427
    user3157427 over 10 years
    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
  • user3157427
    user3157427 over 10 years
    I used Threa.Sleep(2000) to wait 2 seconds, but it hadn't. İs there any difference
  • Justin Harvey
    Justin Harvey over 10 years
    Yes, WebDriverWait lets you wait until a condition occurs, e.g. the element becomes visible.
  • Justin Harvey
    Justin Harvey over 10 years
    So have you looked at the page elements in the browser at the point that this line is executing?
  • Arran
    Arran over 10 years
    Is this even valid Javascript?
  • Andrian Durlestean
    Andrian Durlestean over 10 years
    oops sorry, its a typo, i write it fast, was in hurry, edited
  • Sean Duggan
    Sean Duggan over 8 years
    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!
  • djangofan
    djangofan almost 8 years
    Don't you mean .ElementIsPresent instead of .ElementIsVisible, like the question asked had implied?