Selenium (Python): How to insert value on a hidden input?

10,834

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))
Share:
10,834
Infection
Author by

Infection

Updated on July 02, 2022

Comments

  • Infection
    Infection almost 2 years

    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?

  • Infection
    Infection over 10 years
    Thanks for the response! I tried that (sans the typo for 'execute') but couldn't get the date to appear, even though the script doesn't throw any error. Am I missing something? For clarity, I've added the website I'm trying to work on in my original post.
  • falsetru
    falsetru over 10 years
    @Infection, Sorry, I fixed that typo. And I tested it using test page, and it worked. (the page's hidden field is set as old-value).
  • falsetru
    falsetru over 10 years
    @Infection, Oh, you updated the question with the url. Wait a moment. I will test with that url.
  • falsetru
    falsetru over 10 years
    @Infection, There's so many hidden fields, and no input tag with id form_date_DateTextBox_0.
  • Infection
    Infection over 10 years
    Sorry about that! I truncated the original code to make it readable. The actual ID in question is ita_form_date_DateTextBox_0 (found via Firepath on Firebug). The entire form on that website is dynamically generated by Javascript, which is why you may not be able to find this aforementioned ID by viewing the source code alone.