Home > Enterprise >  Select Element based on HTML Cell Contents with Selenium Java
Select Element based on HTML Cell Contents with Selenium Java

Time:05-04

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>

CodePudding user response:

Try to select the checkbox using the following Xpath :

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