Change value field in html input tag using selenium in python

牧云@^-^@ 提交于 2021-02-10 15:50:00

问题


The 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 to no success.

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

回答1:


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)



回答2:


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)


来源:https://stackoverflow.com/questions/47634093/change-value-field-in-html-input-tag-using-selenium-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!