'Select Element based on HTML Cell Contents with Selenium Java

I have an HTML table with each cell containing a checkbox and a color name. HTML color table

I need to tick the checkbox of any given color. The checkbox itself has no good identifiers indicating what color it's selecting.

I tried:

WebElement color = driver.findElement(By.xpath("//*[text()='Violet']"));
color.click();

Obviously that doesn't work as it selects and sends a click to the text itself.

How can I select the checkbox that's inside the same <td> element?

Here's the HTML:

A snip from Chrome for readability

<td width="25%" valign="top" align="center"><nobr><input type="checkbox" name="489_1111111111" value="55069" onclick="unselectBoth(489)" checked="">Select color:</nobr><br>Dill Green


</td>


Solution 1:[1]

Try to select the checkbox using the following Xpath :

String myColor = "Dill Green";
driver.findElement(By.xpath(".//*[@id='myTableID']//td[contains(.,'"+myColor +"')]//input")).click();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asmoun