Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to click on checkbox filter with selenium Python?

I’m trying to click on a checkbox filter on a website with selenium python. This is a part of its HTML code linked to one of the checkbox options.

<div class="shopee-checkbox" bis_skin_checked="1">
    <label class="shopee-checkbox__control">
        <input type="checkbox" name="" value="Jabodetabek">
        <div class="shopee-checkbox__box" bis_skin_checked="1">
            <i> </i>
        </div>
        <span class="shopee-checkbox__label">Jabodetabek</span>
    </label>
</div>

I tried following, but it didn’t work.

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://shopee.co.id/search?keyword=baju%20laki-laki')

time.sleep(5)
driver.find_element(By.XPATH, "//input[@value='JABODETABEK']").click()

I read the answers to similar questions, mostly they suggest using ‘id’ and ‘name’ to find the element. However, in this input tag, there are only ‘type’ and ‘value’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Here is one way to select that checkbox:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[...]
wait = WebDriverWait(driver, 25)
[..]
wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Jabodetabek"]'))).click()
print('clicked on Jabodetabek')

Selenium documentation can be found here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading