0

So that is what the html looks like, and i want to check the box that contains "4845" in the ID or in the Value, how can i do it ?

<tr id="tr_N031394845">
    <td>
<a href="i.do?data=whatever" onclick="forcePutInHistory=true; ajaxrequest(this.getAttribute('href'),this,true); return false; activecell(this);" title="04845 - B.pippo"> 04845 - B.pippo </a>
</td>
    <td><input class="checkbox" name="selectatm1" type="checkbox" value="N031394845|H" tabindex="5"></td>
<td class="                     icon mar_blabla_a
            "><span>4</span></td>
<td>HALA</td>
<td>03139</td>
<td>4845</td>
    <td class="tdsizemax">Stree 123</td>
<td class="icon crl0" title="Ok"><span>0</span></td>
<td class="icon crl0" title=" Ok"><span>0</span></td>
<td class="icon crl0" title="Ok"><span>0</span></td>


                <td class="tdimporto">4567.77</td>
        <td class="tdimporto">4567.77</td>
<td>31/03/2022 15:50</td>
<td>G20</td>
        <td>31</td>
    <td>04/04/2022 11:13</td>
    </tr>

2 Answers2

0

You can try

checkbox_elem = driver.find_element_by_xpath("//input[contains(@value, '4845') or contains(@id, '4845')]")
if not checkbox_elem.is_selected():
    checkbox_elem.click()
juhat
  • 342
  • 2
  • 10
  • This works but there is one problem, let's say i'm looking for "0440", then what happens is that 2 checkboxes gets selected which are: value="N070624402|H" and value="N070840440|H" What i need is only the second because it must look only for the last 4 digits – lungimiranza Apr 04 '22 at 10:46
  • Does the value always end with "|H"? In that case you can use "0440|H" when searching for the matching checkboxes. – juhat Apr 04 '22 at 10:56
0

To click() on the checkbox that contains 4845 in the Value you can use either of the following locator strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "tr[id^='tr'][id$='4845'] td > input[value*='4845']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//tr[starts-with(@id, 'tr') and contains(@id, '4845')]//td/input[contains(@value, '4845')]").click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352