2

I am trying to clear an input on indeed but the clear() function is not working. Any ideas on how to clear the input box?

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.indeed.com")
#this is not clearning the input
elem2 = driver.find_element_by_id("text-input-where").clear()
cLax
  • 93
  • 1
  • 7

5 Answers5

7

First thing i'd like to point out is you are using

elem2 = driver.find_element_by_id("text-input-where").clear()

clear() – clear the value of any text type element. It doesn’t allow any parameter and its return type is void. So elem2 will be null.

Second, we can use a combination of methods depending upon the application under test.

Sometimes, driver.find_element_by_id("text-input-where").clear() doesn't work, if element is not clicked first. So we can do,

driver.find_element_by_id("text-input-where").click();
driver.find_element_by_id("text-input-where").clear();
driver.find_element_by_id("text-input-where").sendKeys(textContent);

OR we can also use Actions class and Webdriver waits.

elem2 = WebDriverWait(browser, 60).until(EC.visibility_of_element_located((By.ID, 'text-input-where')))
webdriver.ActionChains(driver).move_to_element(elem2).click(elem2).perform()
elem2.clear();

OR as mentioned in this answer, we can simulate CTRL + A and DEL keys.

driver.find_element_by_id("text-input-where").click();
driver.find_element_by_id("text-input-where").send_keys(Keys.CONTROL + "a");
driver.find_element_by_id("text-input-where").send_keys(Keys.DELETE);
Naveen
  • 770
  • 10
  • 22
5

You are not using waits thats also an issue.

So use wait first and as @Sers stated use JavaScript executor

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

    driver = webdriver.Chrome()
    driver.get("https://www.indeed.com")

    WebDriverWait(browser, 60).until(EC.visibility_of_element_located((By.ID, 'text-input-where')))

    elem2 = driver.find_element_by_id("text-input-where")

    driver.execute_script('arguments[0].value = "";', elem2)

Below Java code working for me:

WebDriver driver = new ChromeDriver();
driver.get("https://www.indeed.com");
WebDriverWait waiter = new WebDriverWait(driver, 60);
waiter.until(ExpectedConditions.visibilityOfElementLocated(By.id("text-input-where")));
WebElement element = driver.findElement(By.id("text-input-where"));

((JavascriptExecutor) driver).executeScript("arguments[0].value ='';", element);
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
2

You can also try sending backspace keys:

def clear_text(element):
    length = len(element.get_attribute('value'))
    element.send_keys(length * Keys.BACKSPACE)
1

Use JavaScript to set value to empty string:

driver.execute_script('arguments[0].value = "";', driver.find_element_by_id("text-input-where"))
Sers
  • 12,047
  • 2
  • 12
  • 31
0

this will work for all systems:

from sys import platform
def clear(elem):
  elem.send_keys((Keys.COMMAND if platform == "darwin" else Keys.CONTROL) + "a")
  elem.send_keys(Keys.DELETE)
clear(driver.find_element((By.ID, "id")))

Call the function and pass the webelement as an argument

Abdessabour Mtk
  • 3,895
  • 2
  • 14
  • 21