Home > Net >  Selecting a checkbox based on value with Selenium chromedriver in Python
Selecting a checkbox based on value with Selenium chromedriver in Python

Time:08-06

This is the HTML of the checkbox I am trying to interact with:

 <th>
                                        <input data-control-element="Vol_Year_Selector" type="checkbox" value="2022">
                                        <Label>2022</Label>
                                    </th>
                                    <th>
                                        <input data-control-element="Vol_Year_Selector" type="checkbox" value="2021">
                                        <Label>2021</Label>
                                    </th>
                                    <th>
                                        <input data-control-element="Vol_Year_Selector" type="checkbox" value="2020">
                                        <Label>2020</Label>
                                    </th>
                                    <th>
                                        <input data-control-element="Vol_Year_Selector" type="checkbox" value="2019">
                                        <Label>2019</Label>
                                    </th>
                                    <th>
                                        <input data-control-element="Vol_Year_Selector" type="checkbox" value="2018">
                                        <Label>2018</Label>
                                    </th>

I need to click this checkbox, along with a few other very similar ones. This is the code I have now based on what I was able to find:

    driver.find_element(By.XPATH,"//option[@value='" dl_data "']").click()

where I am iterating through the array dl_data that contains all of the year strings ["2018","2019",....] This doesn't work, as selenium is finding no such element.

Could anyone tell me what I am doing wrong here?

CodePudding user response:

You are looking for option elements, but these are input elements, therefore try:

driver.find_element(By.XPATH,"//input[@value='" dl_data "']").click()
  • Related