Shivam Negi
Shivam Negi

Reputation: 31

How to change value field in html input tag using selenium in python

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)`   

Upvotes: 3

Views: 11416

Answers (2)

Nguyen Tien Huong
Nguyen Tien Huong

Reputation: 11

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)

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193088

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)

Upvotes: 5

Related Questions