4

Here is the HTML:

<li>
<input type="checkbox" checked="" name="selectedMstrPrivGroupList[9].mstrAuthorities[0].status"/>
Add Dexter
</li>

How could this element be clicked in WebDriver? It is a check box. And I want to use XPath as I have close to 30+ check boxes in the page. So that I can create a generic method and pass only the WebElement. I tried the following but didn't work.

Driver.findElement(By.xpath("//input[contains(.,'Add Dexter')]")).click();
2
  • why dont you do it with findElements? Another option could be cssSelectors Commented Sep 11, 2012 at 10:53
  • Another option, but they're not the same thing. Please tag your question correctly. Commented Sep 11, 2012 at 10:55

4 Answers 4

7

If the checkbox next to "Add Dexter" is what you want to click on the page, you can use:

Driver.findElement(By.xpath("//li[contains(.,'Add Dexter')]//input[@type='checkbox']")).click();
Sign up to request clarification or add additional context in comments.

1 Comment

and if you want to make a generic method out of it, "//li[contains(.,'" + someString + "')]//input[@type='checkbox']" should work.
1

What is with this one:

  Driver.findElement(By.xpath("//input[@name='selectedMstrPrivGroupList[9].mstrAuthorities[0].status']")).click();

2 Comments

Thanks for the quick response... This works in this case but I particularly don't want to use the name attribute. Reason: Page is dynamic and associated text changes frequently... So, I was looking for something with which i can click on the check box with its associated text... Thanks again!
This worked for me.. Thanks for the help.. Driver.findElement(By.xpath("//li[contains(.,'Add Dexter')]//input[@type='checkbox']")).click();
1

You can use like this, driver.findElement(By.xpath("//li[contains(text(),'Add Dexter')]")).click()

Comments

1

You can use xpath to click on the element as below:

driver.findElement(By.xpath("//input[text()='Add Dexter']")).click();

You can also click on that element by using cssSelector instead of xpath as below:

driver.findElement(By.cssSelector("input:contains(^Add Dexter$)")).click();

Note: CssPath/CssSelector is faster than xpath. So it's better to use cssSelector than xpath in most cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.