Home > Blockchain >  How to click on checkbox filter with selenium Python?
How to click on checkbox filter with selenium Python?

Time:02-05

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  bis_skin_checked="1">
    <label >
        <input type="checkbox" name="" value="Jabodetabek">
        <div  bis_skin_checked="1">
            <i> </i>
        </div>
        <span >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 laki-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'.

CodePudding user response:

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.

CodePudding user response:

@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()
  • Related