问题
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