AnOldSoul
AnOldSoul

Reputation: 4197

Element not visible for check box though its available

I am trying to tick a checkbox using Selenium. The page looks like below.

enter image description here

Then when I click on edit, it changes to the below shown format.

enter image description here

At this time, I can select the checkbox from Chrome dev tools using the below shown xpath.

//input[@name='value[326071]']

Below is how the DOM looks.

enter image description here

I have put a thread wait before clicking on the check box. Below is my code.

Thread.sleep(5000);
WebElement assignWorkSpaceElement = chromeDriver.findElement(By.xpath("//input[@name='value[326071]']"));
assignWorkSpaceElement.click();

I tried with the below xpath as well. Still no luck. I am debugging the code through IntelliJ and prior to going over the web element line, I am able to select the checkbox from Chrome dev tools. Its clearly there.

//*[contains(@class,'col-md-8')]//input[@name='value[326071]']

but when I execute the code, I get the following exception.

org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: chrome=70.0.3538.110)
  (Driver info: chromedriver=2.40.565386 (45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.6 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

What am I doing wrong here? Any help would be much appreciated.

Upvotes: 1

Views: 316

Answers (2)

koushick
koushick

Reputation: 497

I Think you need to change your xpath,and don't use thread.sleep instead of that use webdriver wait.

WebDriverWait wait = new WebDriverWait(driver, 5);
  WebElement e4 = wait.until(ExpectedConditions.visibilityOf(By.xpath("//div[@class='col-md-8- form-control-static']//following::input[1]")));

if(e4.isDisplayed())
{
  e4.click();
}

Upvotes: 1

Moshe Slavin
Moshe Slavin

Reputation: 5204

As far as I can see in your HTML it looks like the XPath you are locating has multiple elements...

Now, If you are trying to loop over all checkbox's and click that can be done with:

WebElement assignWorkSpaceElement = chromeDriver.findElements(By.xpath("//input[@name='value[326071]']"));
for (WebElement el : assignWorkSpaceElement ) {
     el.click();
}

But if you just want the one element with the value=326071 you must add some more specific XPath something like:

WebElement assignWorkSpaceElement = chromeDriver.findElement(By.xpath("//input[@name='value[326071]' and @type='checkbox' ]"));
assignWorkSpaceElement.click();

Or:

WebElement assignWorkSpaceElement = chromeDriver.findElement(By.xpath("//input[@name='value[326071]' and @value='1' ]"));
assignWorkSpaceElement.click();

Hope this is helpfull!

Upvotes: 2

Related Questions