-1

I'm trying to use selenium to click on a checkbox, as is depicted in the HTML of the picture. The think is, the checkbox input tag is the same for all four options, and they are distinguished only through the id="country_england" ... . I'm not sure how I can write code to select the england box and click it. Any help appreciated.

HTML Code:

HTML Code

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rye Rye
  • 57
  • 5

2 Answers2

0

use xpath:

 //b[@id="country_england"]/../input

This finds the p tag that has that ID and then goes to the parent element and navigates back to input element

you can also use:

      //b[@id="country_england"]/preceding-sibling::input

This finds the preceding element of p tag that has tag input

Code would be :

webD.get("https://icem.data-archive.ac.uk/#step1")

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@value="1861"]'))).click()


WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
    (By.XPATH, '//b[@id = "country_england"]/preceding-sibling::input'))).click()
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • boxEng = webD.find_element_by_xpath('//p[@id="country_england"]::preceding-sibling/input') boxEng.click() Code still not successful.. Can you see a problem? Thank you for your help!! – Rye Rye Jan 30 '21 at 20:32
  • `webD.find_element_by_xpath('//p[@id="country_england"]/preceding-sibling::input').click()` – PDHide Jan 30 '21 at 20:34
  • @RyeRye updated the answer with code also – PDHide Jan 30 '21 at 20:35
  • I really really appreciate your help!! But still no success.. Is there any additional info I could give you that would help? In any case I do see how this is the right direction, it feels so close... – Rye Rye Jan 30 '21 at 20:42
  • Could you give the website URL ? Is the element inside iframe ? – PDHide Jan 30 '21 at 22:01
  • https://icem.data-archive.ac.uk/#step1 this is the website. i successfully coded the years element. thank you!! – Rye Rye Jan 30 '21 at 22:07
  • i miss read b as p thats why the locattor didn't work – PDHide Jan 30 '21 at 22:31
0

The desired element is a Angular element. So to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using XPATH and preceding:

    driver.get("https://icem.data-archive.ac.uk/#step1")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='1851']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[@class='ng-binding' and @id='country_england']//preceding::input[1]"))).click()
    
  • Using XPATH and preceding-sibling:

    driver.get("https://icem.data-archive.ac.uk/#step1")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='1851']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[@class='ng-binding' and @id='country_england']//preceding-sibling::input[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:

icem_data-archive


Update

icem

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Wow thank you so much for your extensive response. Sadly still unsuccessful. If you wish to take a closer look, the website is the following: https://icem.data-archive.ac.uk/#step1 Thank you for your help!! – Rye Rye Jan 30 '21 at 22:13
  • Could you elaborate the suggestion? Not sure how to implement it. But I don't think I can... I'll sleep on it and have a look with fresh eyes tomorrow. Thank you again, if you have any other ideas i'd love to try them out :) – Rye Rye Jan 30 '21 at 22:31
  • @RyeRye Checkout the updated answer and let me know the status. – undetected Selenium Jan 30 '21 at 22:47
  • Tired eyes indeed... Yes I was able to, as the code for the input has a unique value to identify each checkbox in the years. I used classic xpath and click combo. Thank you! Will try out now :) – Rye Rye Jan 30 '21 at 23:00
  • PDHide suggested the following code: WebDriverWait(webD, 20).until(EC.element_to_be_clickable( (By.XPATH, '//b[@id = "country_england"]/preceding-sibling::input'))).click() and it was successful. Thank you very much for your help :) I can finally say... good night! – Rye Rye Jan 30 '21 at 23:06