Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 397
user3157427 Avatar asked Dec 05 '22 07:12

user3157427


1 Answers

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]")));
    
like image 176
Neil Avatar answered May 10 '23 10:05

Neil