0

I need to check a checkbox, but am not able to do that. It's giving the error: Element is not visible

This is the element:

<input _ngcontent-c5="" class="lb-checkbox ng-untouched ng-pristine ng-valid" type="checkbox" id="1" ng-reflect-model="false">
::after </input>

And this is the code I tried:

driver.FindElement(By.Id("1")).Click()
  • 1
    What hv you tried? Edit and add your code. – Bharat Mane May 15 '18 at 12:19
  • 4
    First of all you shouldn't try to manipulate an element that isn't visible since it doesn't represent user behaviour. If you really want to do it try ((JavascriptExecutor)driver).executeScript("arguments[0].click()", yourWebElement); but like I said, I'd recommend to go for a whatever triggers the hidden element instead. – Daniel May 15 '18 at 12:30
  • 2
    Exactly what @godisalie92 says: why do you think that performing action which user cannot do will make a valid e2e test? What you want to accomplish? – Peter M. - stands for Monica May 15 '18 at 13:51
1

First verify the checkbox is already selected or not by using the if condition. Use this below code

boolean iconEnabled = driver.findElement(By.id("1")).isEnabled();

if(iconEnabled == true)
{

    WebElement checkbox = driver.findElement(By.id("1"));
    checkbox.click();

}
|improve this answer|||||
0

You can use JavascriptExecutor to change the display attribute to make the element visible then perform the click.

JavascriptExecutor executor4= (JavascriptExecutor)driver;        
executor4.executeScript("document.getElementById('1').style.display='block';"); 
driver.FindElement(By.Id("1")).Click();

Above code make the checkbox visible so you can click on it.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.