clear() does not clear the textbox with selenium and python and firefox
12,709
Solution 1
To clear the textbox you need to induce WebDriverWait with expected_conditions
set to element_to_be_clickable
, next invoke click()
on the WebElement and then invoke clear()
as follows :
# clear the search text box
searchForMovie = WebDriverWait (driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#search-text")))
searchForMovie.click()
searchForMovie.clear()
Solution 2
I think it should be as easy as:
driver.execute_script("$('#search-text').val('');")
If you want to do it without jQuery, you could use plain javascript:
document.getElementById('search-text').value = '';
Solution 3
In case .clear()
does not work you can try:
from selenium.webdriver.common.keys import Keys
#...your code (I was using python 3)
driver.find_element_by_id('foo').send_keys(Keys.CONTROL + "a");
driver.find_element_by_id('foo').send_keys(Keys.DELETE);
Author by
LetzerWille
Updated on June 05, 2022Comments
-
LetzerWille almost 2 years
clear()
does not work in this case. I am getting append after append.
searchForMovie.clear()
is not working... I have also tried to send
CTRL + 'a'
, and then theDELETE
. Again all I got are just appends...for movie in allMissing: time.sleep (10) searchForMovie = WebDriverWait (driver, 10).until \ (EC.presence_of_element_located ((By.CSS_SELECTOR, "#search-text"))) searchForMovie.send_keys (movie) # click enter = WebDriverWait (driver, 10).until \ (EC.presence_of_element_located ((By.CSS_SELECTOR, "#search-submit"))) driver.execute_script ("arguments[0].click()", enter) # clear the search text box searchForMovie = WebDriverWait (driver, 10).until \ (EC.presence_of_element_located ((By.CSS_SELECTOR, "#search-text"))) searchForMovie.clear()
-
LetzerWille about 6 yearsI got this error message, after trying your solution. The element reference of <input id="search-text" name="nm" type="text"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
-
undetected Selenium about 6 yearsOnce you
searchForMovie.send_keys(movie)
and subsequentlydriver.execute_script ("arguments[0].click()", enter)
the HTML DOM is bound to change. So you need to come back to the same url where you started looking out forsearchForMovie = WebDriverWait (driver, 10).until \ (EC.presence_of_element_located ((By.CSS_SELECTOR, "#search-text")))
-
Jawad Ahmad Khan over 4 yearsThis is not working, i tried it on Instagram login.
-
Chuck over 3 yearsThis solution does not appear to trigger a 'change' event.
-
Chuck over 3 yearsAlso, the change happens asynchronistically with subsequent python code.
-
Admin over 3 yearsSo in what context does that clear() API entry point function by design? It's clearly some kind of special-casing, but not documented accessibly to us.
-
myworldbox over 2 yearsnot working pls stop giving wrong answer