[Python] HOWTO Set date type field using Selenium (Python)

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    The code below shows the attributes of the field.

    I must have gone through at least 20 ways on how to set the value of the field using Selenium.

    Examples I found on the Internet but none of them works. I have provided a screenshot of how the field looks on the page. I can navigate to the field but I have not been able to put in any dates using Selenium. I can type in it manually or I can click the calendar icon to the right and set the date by selecting from the Calendar. [​IMG]

    element = driver.find_element(By.XPATH, "/html/body/div[1]/div/form/label[1]/input")
    attrs = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', element)
    pprint(attrs)



    OUTPUT:

    {'class': 'border-2 p-2 rounded-md w-full ',
    'name': 'dob',
    'placeholder': 'Date of Birth',
    'type': 'date'}



    I need to be able to set the date using Selenium (Python code please or Python + JS).
    Related Q: I couldn't figure out how to get the right element to click for the recaptcha as well (it doesn't pop-up a picture set if I have visited it before so the captcha itself is not an issue, but I tried the selector, XPath, Full Xpath and all resulted in an error that Selenium couldn't find the element.)

    Example page: https://amoe.onecountry.com/enter?productId=2025_Aldean-WestPalm

    Continue reading...

Compartilhe esta Página