Need help clicking on this checkbox

All checkboxes have the same id, but the id for the parent element of each is different.

<a id="file1"...<div<div<input id="checkbox-input"

Can you help me with the syntax to click on it?

Hi Julio,

When you get things like this it’s usually easiest to use xpath (because you can test your xpaths with dev tools in your browser first and then put the working xpath into your test)

without the actual html I can’t give you an exact answer but from what you supplied something like //a[@id="file1"]//input[@id="checkbox-input"] should probably work.

Dave.

Thank you! Your suggested xpath works
Out of curiosity, can I pull this off with css? I have a situation where I have the exact same structure except the first element is a tr tag instead of a (but the IDs are the same), and ideally would like to use the same keyword

Hi Julio,

Thanks for encouraging me to learn how to use CSS selectors (I never really used them).

It seems you use a > to specify an element is directly below another, but if you just use a space then it can be any level below the first element. I found a good CSS Selectors Reference

So If I understand it correctly, the CSS equivalent of //a[@id="file1"]//input[@id="checkbox-input"] could be

  • #file1 #checkbox-input
  • or if you want to be more specific a#file1 input#checkbox-input

Meaning if you don’t car if they are <a> or <tr> tags you could just use

  • #file1 #checkbox-input

but if you want to distinguish between the <a> and <tr> tags you can do

  • a#file1 input#checkbox-input and tr#file1 input#checkbox-input
    or even
  • a#file1 #checkbox-input and tr#file1 #checkbox-input

Likewise if you want to hit the <tr> tags with the xpath it’s simply //tr[@id="file1"]//input[@id="checkbox-input"]

Also if you don’t care if it’s a <a> or <tr> tags you can also do //*[@id="file1"]//input[@id="checkbox-input"] with xpath

I also found out you can test the CSS selectors in dev tools too which means I’ll use them more in the future.

Hope that helps,

Dave.

Thanks, worked beautifully

1 Like