2

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>

2
  • Can you post the html of the element in question. What you should be doing is clicking the checkbox which is located beside the text. Commented May 3, 2022 at 3:43
  • I added the HTML to the question. I don't have any good way to identify the checkbox, I don't think. It has a unique "value" that correlates to the color, but I'm not completely sure of that and I also have no control over it. Can I find the color name with an xpath contains() and then use preceding-sibling to get the checkbox? Commented May 3, 2022 at 4:07

1 Answer 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();
Sign up to request clarification or add additional context in comments.

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.