0

I'm trying to use python & Selenium to click on a slider/checkbox on a website and I'm having a lot of difficulty. I can simply find the element by ID (1369) and click on it no worries, except the ID numbers change dynamically over time so they are proving pretty much useless to me.

I also tried finding the element by class, XPath, etc. and clicking on it using many questions posted to Stack Overflow as reference with no luck, as there seems to be some differences in the HTML code in my scenario that are stopping me from progressing. Any help would be greatly appreciated thank you!

Reference HTML as text:

<div class="f-box vcbox">
  <span class="text-item box-item">Single date</span>
  <div id="1369" class="checkbox checbox-switch switch-primary  ">
    <label>
      <em class="fake-input  "></em>
      <span>
        ::before
      </span>
    </label>
</div>

Reference HTML code on website

Code that works when the ID is known in advance:

driver.find_element(By.ID, "1369").click()

Code that hasn't worked:

driver.find_element(By.CSS_SELECTOR, 'em.checkbox checbox-switch switch-primary').click()

driver.find_element(By.CSS_SELECTOR, 'div.checkbox.checbox-switch.switch-primary').click()

driver.find_element(By.XPATH, "//span[contains(.,'Single date')]").click()
7
  • 2
    I also tried finding the element by class/xpath This should work, with the right xpath expression. Show us what you tried. Commented May 2, 2024 at 3:52
  • 1
    Screenshots of the UI are great, screenshots of code or HTML are not. Please read why a screenshot of code/HTML is a bad idea. Edit your question and add the code/HTML as text, properly formatted. In addition, a link to the page would be really helpful in cases like this. Commented May 2, 2024 at 3:56
  • 2
    @ me after you've updated your question with the HTML as text. I have an answer but I want to test it first. Commented May 2, 2024 at 4:02
  • Hi all. Thanks for your input. I've updated my post to include what you've asked for. If there's anything that can be improved from my end please let me know and I'll be happy to edit again as required. Commented May 2, 2024 at 4:46
  • @JeffC happy to try out any suggestions thank you but the website requires a logon that I can’t give out for others to try testing sorry mate. Commented May 2, 2024 at 6:13

1 Answer 1

0

Given the HTML

<span class="text-item box-item">Single date</span>
<div id="1369" class="checkbox checbox-switch switch-primary  ">

We have an element nearby that can be uniquely identified using contained text,

<span class="text-item box-item">Single date</span>

using and XPath

(By.XPATH, "//span[text()='Single date']")

From there, we can find the DIV right below it using following-sibling,

(By.XPATH, "//span[text()='Single date']/following-sibling::div[1]")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.