'Python - Selenium - Checkbox by contains value
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>
Solution 1:[1]
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()
Solution 2:[2]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | juhat |
| Solution 2 | undetected Selenium |
