3

I have been struggling for days to find a way to set a date of a datepicker using Selenium with Python. I found out that there is a hidden element with the date, so I tried javascript executor to set a value to that hidden element. I tried different ways, and the scripts seem to execute fine, but the date doesn't change.

My script looks like this:

#input date by using hidden date method 1
element = browser.find_element_by_xpath("/html[1]/body[1]/div[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/table[1]/tbody[1]/tr[2]/td[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[1]/td[2]/input[1]")
browser.execute_script("arguments[0].setAttribute('value', '{0}')".format("2019-10-31"), element )
element.submit()

and

#input date by using hidden date method 2
date = "2019-10-31"
element= browser.find_element_by_xpath("/html[1]/body[1]/div[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/table[1]/tbody[1]/tr[2]/td[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[1]/td[2]/input[1]")
browser.execute_script('arguments[0].setAttribute("value", "%s")' % date, element)
element.submit()

The HTML code and picture of the website looks like this:

HTML of date picker: HTML code of date picker

Date picker element:

Date picker

Edit:

Updated HTML with the hidden element visible HTML Code of visible element

  • I would say what you're trying to do is far from ideal - it's basically modifying hidden/private behaviour in tests which is generally a bad idea in any kind of test. But anyway, to answer your question, have you tried setting the value instead? That's what is sent by the form, not the element attribute. eg: `browser.execute_script('arguments[0].value = "%s"'...` (as to why you shouldn't, you can see it yourself: you're finding it hard to debug this case) – Christian Nov 29 '20 at 20:30
  • @Official_Ali Is it a public website we can test? – undetected Selenium Nov 29 '20 at 21:17
  • @Christian I tried the code as you pointed out, but nothing changed. ```browser.execute_script('arguments[0].value = "%s"' % date, element)``` – Official_Ali Nov 29 '20 at 21:45
  • @DebanjanB No unfortunately it's not :/ – Official_Ali Nov 29 '20 at 21:46

1 Answers1

1

The <input> element is having the type attribute set as hidden. You can edit/remove the type attribute and set the value as follows:

element = browser.find_element_by_xpath("/html[1]/body[1]/div[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/table[1]/tbody[1]/tr[2]/td[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[1]/td[2]/input[1]")
browser.execute_script("arguments[0].removeAttribute('type')", element)
new_element = browser.find_element_by_xpath("/html[1]/body[1]/div[1]/form[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/table[1]/tbody[1]/tr[2]/td[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[1]/td[2]/input[1]")
browser.execute_script("arguments[0].setAttribute('value','2019-10-31')", new_element)

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried your suggestion, the code works and you see a new element with the input box on the website, but the date doesn't change. I tried changing setAttribute to value but that didn't work either. ```browser.execute_script("arguments[0].Value = '2019-10-31'", new_element)``` – Official_Ali Nov 29 '20 at 21:58
  • @Official_Ali It's not a new element but the hidden element. Can you update the question with the text based HTML of the `` element when it's visible? – undetected Selenium Nov 29 '20 at 22:03
  • Just updated my question with a screenshot of the visible element with HTML – Official_Ali Nov 29 '20 at 22:10
  • @Official_Ali Checkout the updated answer and let me know the status. – undetected Selenium Nov 29 '20 at 22:16
  • I am getting an error: JavascriptException: Message: javascript error: Cannot read property 'setAttribute' of null. Also instead of setattribute I tried value, but also gives me a null error. – Official_Ali Nov 29 '20 at 22:30
  • @Official_Ali There was a bug which I've removed now. Retest and let me know the status please. – undetected Selenium Nov 29 '20 at 22:33
  • Got a timeout error. Your first piece of code worked better as I was still able to change the date when I just tried, however the dateviewer below did not change with it. I tried to execute the report to see if the output actually shows the date that was set in the hidden element made visible. Will let you know, thanks – Official_Ali Nov 29 '20 at 22:52
  • @Official_Ali You want me to revert back the answer to it's initial version? – undetected Selenium Nov 29 '20 at 22:53
  • Yes please, that one seems to work, but will be verified after I receive the report. WIll update you tomorrow. Thanks – Official_Ali Nov 29 '20 at 22:55
  • @Official_Ali Minor change, retest once please. – undetected Selenium Nov 29 '20 at 22:58
  • Still a timeout message received as well as the hidden element not showing up. The previous code seems to work as the output of the report is correct. Please revert back to the code without the wait. – Official_Ali Nov 30 '20 at 07:02
  • @Official_Ali Rolled back to it's initial version. – undetected Selenium Nov 30 '20 at 07:18
  • 1
    Just retested, and worked fine. Initially it didn't work for me because I forgot to switch frame. Your code works perfect, thanks – Official_Ali Nov 30 '20 at 07:47