2

The following html tag:

<input id="input-value" title="Search" type="text" value="">   

I want to change the value attribute from "" to "foo".

<input id="input-value" title="Search" type="text" value="foo">

I am trying this with send_keys() with no success.

ele = browser.find_element_by_id("input-value")
ele.send_keys("foo")
ele.send_keys(Keys.RETURN)`   
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Shivam Negi
  • 21
  • 1
  • 2
  • What exactly are you trying to achieve? Changing the contents of an input via the UI doesn't change the underlying `value` attribute. – Daniel Roseman Dec 04 '17 at 13:11
  • Yes, figured that out. Thanks @DebanjanB. I am trying to change the contents of a search input tag. The website(web.whatsapp) is using react.js. I am trying to search by name using the field. – Shivam Negi Dec 04 '17 at 15:26

2 Answers2

4

To edit the value attribute and assign the value foo to it you can use the following code block which uses the JavascriptExecutor :

ele = browser.find_element_by_css_selector("input#input-value")
browser.execute_script("arguments[0].setAttribute('value','foo')", ele)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

Using .click() before .send_keys() like:

ele = browser.find_element_by_id("input-value")
ele.click()
ele.send_keys("foo")
ele.send_keys(Keys.RETURN)