Hidden Input Field with WebDriver in Python


How to update hidden input value field value with WebDriver in Python?

✍: FYIcenter.com


Normally, a hidden input value field is used to hide some value in the Web page. The hidden value is passed back to the Web server when the page is submitted. You should not change the hidden value.

But if you really want to update the hidden value, you have 2 options:

  • Use JavaScript code to modify the hidden value.
  • Use JavaScript code to make the input visible and remove the hidden value. Then call send_keys() to enter the new value.

Here is an example program that uses both options to update hidden values

# UpdateHiddenValue.py
# Copyright (c) FYIcenter.com 
from selenium.webdriver import Chrome

driver = Chrome()

# update hidden value directly 
input = driver.find_element_by_name("cost")

# make input visible with no value, and enter new value
input = driver.find_element_by_name("margin")

# submit the form
form = driver.find_element_by_tag_name("form")

# check submitted values


To test the above program, you can use the following HTML file, HiddenValue.html:

   <p>Form with with hidden values:</p>
   <form method="get" action="submit.html">
    Cost: $?<input name="cost" type="hidden" value="10.00"/><br/>
    Margin: %?<input name="margin" type="hidden" value="50.00"/><br/>
    Price: $<input name="price" type="text" value="20.00"/><br/>

Run the program:

C:\fyicenter> python UpdateHiddenValue.py


The output shows that both options work correctly.

Note that you don't change the input type from "hidden" to "text', send_keys() will give you the "selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable" error.

You also need to remove the existing value with "arguments[0].value='';" script. Otherwise, send_keys() will append new value to the old value.


Disabled Input Field with WebDriver in Python

Load HTML Files with WebDriver in Python

Using Selenium WebDriver Client Python API

⇑⇑ Selenium Tutorials

2023-07-24, 12603🔥, 1💬