Home > Enterprise >  Python - Selenium - Checkbox by contains value
Python - Selenium - Checkbox by contains value

Time:04-04

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  name="selectatm1" type="checkbox" value="N031394845|H" tabindex="5"></td>
<td ><span>4</span></td>
<td>HALA</td>
<td>03139</td>
<td>4845</td>
    <td >Stree 123</td>
<td  title="Ok"><span>0</span></td>
<td  title=" Ok"><span>0</span></td>
<td  title="Ok"><span>0</span></td>


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

CodePudding user response:

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()

CodePudding user response:

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()
    
  • Related