arsenal88
arsenal88

Reputation: 1160

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?

Upvotes: 3

Views: 6507

Answers (2)

frianH
frianH

Reputation: 7563

Try using .execute_script:

date_element = driver.find_element_by_name('tb_date')
driver.execute_script("arguments[0].value = '2020-07-01';", date_element)

Upvotes: 7

Justin Lambert
Justin Lambert

Reputation: 978

You can clear and then sendkeys to that field.

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

Upvotes: 5

Related Questions