Regular expressions may not be the best solution, but it should work.
import re def get_id(str, html_page): # str in this case would be 'polishpottery' return re.search(r'<input[^<>]*?type="checkbox"[^<>]*?id="([A-Za-z0-9_ -]*?)"[^<>]*?> ?' + str, html_page).group(1) id = get_id('polishpottery', html) checkbox = driver.find_element_by_id(id) checkbox.toggle() # Or, more minimallistically: driver.find_element_by_id(get_id('polishpottery', html)).toggle()
Conclusion:
>>> print(html) <li id="product_cat-52"> <label class="selectit"> <input value="52" type="checkbox" name="tax_input[product_cat][]" id="in-product_cat-52"> polishpottery </label> </li> >>> get_id('polishpottery', html) 'in-product_cat-52'
source share