0

Can't get the checkbox clicked using python selenium, geckodriver.

The input line in the following html (from inspect-element option) appears grey in color and does not highlight the checkbox.

<div class="ui_checkbox BZGrt w">
 <input id="checkbox_590" type="checkbox" class="input" value="10697">
 <label class="label" for="checkbox_590" >
  ::before
  <div class="icWwC f u k w"><span class="PDJtN"><span class="mTKbF">Vegan Options</span></span></div></label></div>

The checkbox is highlighted when cursor is over ::before

My python code is the following:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#link = link_list[0]
link = "https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html"
driver = webdriver.Firefox() 
time.sleep(2)
driver.get(link)
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
driver.find_elements(By.XPATH, '//*[@id="checkbox_590"]').click()

Please help with getting that checkbox clicked.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
R Sandy
  • 25
  • 3

3 Answers3

0

You can find by text, it is not the best approach but it works fine.

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Set up driver
driver = webdriver.Firefox()
driver.get("https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html")

# Wait for the cookie consent button to be clickable and click it
cookie_consent_button = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler')))
cookie_consent_button.click()

# Generate GUI path
gui_path = '//span[text()="Vegetarian Friendly"]'

# Find and click on "Vegetarian Friendly"
element = driver.find_element(By.XPATH, gui_path)
element.click()

# Close the browser
driver.quit()
0

You can find by for of label, it is not the best but i try it works.

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

#link = link_list[0]
link = "https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html"
driver = webdriver.Firefox() 
time.sleep(2)
driver.get(link)
driver.implicitly_wait(10)
driver.find_element(By.XPATH,"//label[@for='checkbox_590']").click()
0

To click on the associated with the <label> Vegan Options you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

  • Using XPATH:

    driver.get("https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Vegan Options"]//ancestor::label[1]'))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

Vegan Options

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352