How to click on checkbox filter with selenium Python?

Question:

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’.

Asked By: yaee

||

Answers:

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.

Answered By: Barry the Platipus

@value for the checkbox in the xpath is incorrect. The selenium web locator techniques are case sensitive. So make sure to use exact values while identifying objects. Changing your xpath to below would fix the issue.

driver.find_element(By.XPATH, "//input[@value='Jabodetabek']").click()
Answered By: Srinivasu Kaki