1

I am trying to change the value of an input tag element.

Here is the tag: <input type="hidden" id="hiRequestAccessType" data-bind="value: requestAccessTypeStr" value="2">

I want to change value to "2,1".

Based on discussion at Set value of input instead of sendKeys() - selenium webdriver nodejs, I tried using execute_script, but value remains unchanged.

I tried this:

passwordcheck_input_element = driver.find_element_by_xpath('//*[@id="hiRequestAccessType"]') . ###THIS DOESNT THROW ERRORS
new_value = "2,1"

driver.execute_script("arguments[0].value = arguments[1].toString()", passwordcheck_input_element, new_value)
# driver.execute_script("arguments[0].value = '" + new_value + "'", passwordcheck_input_element) . ###TRIED THIS IN LIEU OF ABOVE

For either alternative, code runs but value remains unchanged from visual inspection. I also tried the above two alternatives using 'setAttribute' instead of directly, same (no change) result.

Note that the webpage is a form where clicking on a check box changes value to "2,1" as desired. (But if I try finding the check box element, I get the message it is not clickable, hence this route).

Now, what's weird is I know it's doing something right behind the scenes because I tried querying value attribute before and after my execute_script call and it prints out the new value correctly for latter. However, as I said, the UI doesnt show this change; further, when I move on and hit the submit buttom further down, it's the old value that gets used because I am not getting the page that should load if the new value were used.

Titanical
  • 45
  • 1
  • 2
  • 9

2 Answers2

3

Could you please try the code below?

passwordcheck_input_element = driver.find_element_by_id("hiRequestAccessType")
driver.execute_script("arguments[0].value = '2,1';", passwordcheck_input_element) 

You can also control the checkbox object via javascript execution if it is not clickable.

driver.execute_script("document.getElementById('hiRequestAccessType').checked = true;")
Recep Duman
  • 123
  • 9
  • 1
    `selenium.common.exceptions.WebDriverException: Message: unknown error: input_value is not defined` – andilabs Apr 08 '19 at 12:04
-2

driver.find_element_by_xpath('//*[@id="hiRequestAccessType"]').setAttribute("value", "1")

Short you can select with xpath or css name element after selection you can change with .setAttribute function your value. Also you can get current selected element value with getAttribute function.For simulate checkbox click : .setAttribute("checked", "checked")

Servoo
  • 1
  • 1
  • I think you should review your answer I get `AttributeError: 'WebElement' object has no attribute 'set_attribute' ` when using your solution – Nwawel A Iroume Jan 26 '21 at 17:50