nic.jess
nic.jess

Reputation: 55

I am not able to check checkbox by selenium Web driver

Please see the code given below and help me in finding Xpath. I am new to selenium.

<label class="checkbox">
<input class="chk-input" type="checkbox" data-bind="value: Value" value="3806">
<span data-bind="text: Text">GYM</span>
</label>

Upvotes: 0

Views: 1073

Answers (2)

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Hi to select check box please do it like below

 driver.findElement(By.xpath("//*[@value='3806']")).click();

UPDATE

WebDriverWait wait = new WebDriverWait (drv,20);
    // for location
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("location")));
    drv.findElement(By.xpath("location")).click();
    // for Position
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Position")));
    drv.findElement(By.xpath("Position")).click();

Upvotes: 0

Tom Trumper
Tom Trumper

Reputation: 472

To locate the checkbox element with an XPath expression, use the following format:

//tagName[@attribute='value']
  • // indicates to search the entire DOM for the required element.

  • tagName specifies what type of element you're looking for. For your example it would be input.

  • [@attribute='value'] is a predicate to only return elements who have an attribute equal to the specified value. For your example you could use [@value='3806'] or [@class='chk-input'].

So pieced together, we can use the following as an XPath expression to find your checkbox:

//input[@value='3806'] or //input[@class='chk-input']

Finally, to click the element you can simply use WebElement.click() like so:

WebElement checkbox = driver.findElement(By.xpath("//input[@value='3806']"));
checkbox.click();

Upvotes: 0

Related Questions