BigD
BigD

Reputation: 77

Select checkbox using Selenium with Python3.x and Selenium

I'm trying to check the checkbox on a page: https://www.pkobp.pl/poi/?clients=1,2,3.

<li class="poi-filter-top__el">
   <div class="poi-icon poi-icon--facility"></div>
   <input type="checkbox" id="poi-legend-facility" class="js-poi-legend" name="type" value="facility"> <label for="poi-legend-facility" class="poi-legend input-checkbox poi-filter-top__label">Oddział</label>  <a href="#facility" class="js-poi-action poi-filter-top__link" data-action="facility">Wybierz rodzaj</a>  
</li>

I try to do this with:

checkboxes = driver.find_elements_by_id("poi-legend-facility")

for checkbox in checkboxes:

    if not checkbox.is_selected():

        checkbox.click()

But it doesn't work. Can you help me?

Upvotes: 3

Views: 2245

Answers (3)

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

You have only one checkbox with such ID (poi-legend-facility).

You can do like this:

checkbox = driver.find_element_by_id("poi-legend-facility")

if not checkbox.is_selected():

   checkbox.click()

Or in your case try this code:

checkboxes = driver.find_elements_by_id("poi-legend-facility")
checkboxes[0].click()

PS: Using ID for finding element faster than finding by XPath.

Upvotes: 5

iamsankalp89
iamsankalp89

Reputation: 4739

Try this xpath

checkElements= driver.find_element_by_xpath("//input[@type='checkbox' and @value='facility']")
checkElements.click()

I don't know python so might be syntax error but path is correct

Upvotes: 0

SIM
SIM

Reputation: 22440

This is the the only way I could find to check all the boxes in that page and break out of loop. Give this a shot as well.

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('https://www.pkobp.pl/poi/?clients=1,2,3')
for tickbox in driver.find_elements_by_css_selector(".input-checkbox"):
    try:
        tickbox.click()
        time.sleep(7)
    except:
        break
driver.quit()

Upvotes: 1

Related Questions