Search code examples
pythonseleniumdatepickerwebdriverwaitdopostback

How to change the value of datepicker using Selenium and Python


In a table element I have the following for a date picker:

<input name="tb_date" type="text" value="2020-07-15" onchange="javascript:setTimeout('__doPostBack(\'tb_date\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="tb_date" class="align-center" style="font-size:14pt;width:120px;">

I can get the input element as follows:

date_element = driver.find_element_by_name('tb_date')

That is fine. But when I try to change the value on this element, it seems to append to the current value.

date_element.send_keys('2020-07-01')
date_element.click()

So the datepicker has a value of '2020-07-152020-07-01'

How can I delete the value attribute and input a new one?


Solution

  • You can clear and then sendkeys to that field.

    date_element.clear();
    date_element.send_keys('2020-07-01')
    date_element.click();