I'm using Selenium's WebDriver and coding in Python.
There's a hidden input field in which I'm trying to insert a specific date value. The field originally produces a calendar, from which a user can select an appropriate date, but that seems like a more complicated endeavour to emulate than inserting the appropriate date value directly.
The page's source code looks like this:
<div class="dijitReset dijitInputField">
<input id="form_date_DateTextBox_0" class="dijitReset" type="text" autocomplete="off" dojoattachpoint="textbox,focusNode" tabindex="0" aria-required="true"/>
<input type="hidden" value="2013-11-26" sliceindex="0"/>
where value="2013-11-26"
is the field I'm trying to inject a value (it's originally empty, ie: value=""
.
I understand that WebDriver is not able to insert a value into a hidden input, because regular users would not be able to do that in a browser, but a workaround is to use Javascript. Unfortunately that's a language I'm not familiar with. Would anyone know what would work?
You can use WebDriver.execute_script
. For example:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://jsfiddle.net/falsetru/mLGnB/show/')
elem = driver.find_element_by_css_selector('div.dijitReset>input[type=hidden]')
driver.execute_script('''
var elem = arguments[0];
var value = arguments[1];
elem.value = value;
''', elem, '2013-11-26')
UPDATE
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://matrix.itasoftware.com/')
elem = driver.find_element_by_xpath(
'.//input[@id="ita_form_date_DateTextBox_0"]'
'/following-sibling::input[@type="hidden"]')
value = driver.execute_script('return arguments[0].value;', elem)
print("Before update, hidden input value = {}".format(value))
driver.execute_script('''
var elem = arguments[0];
var value = arguments[1];
elem.value = value;
''', elem, '2013-11-26')
value = driver.execute_script('return arguments[0].value;', elem)
print("After update, hidden input value = {}".format(value))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With