Search code examples
pythonselenium-webdriverxpathcss-selectorsnosuchelementexception

Selenium Python | Type in input


enter image description here

I am trying to type into the input category using selenium on Python google chrome. I would like to send the keys 1234 to test the configuration, however, I am receiving errors. I believe I am missing something from the code... Sorry if this is super basic, I am still learning selenium... I tried using the @class already and received an error.

driver.find_element_by_xpath("//input[='']/parent::div").send_keys('1234')

I am getting this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[9]/div/div/div/div[1]/div/div/div/div/div[2]/div[1]/div[1]/div[1]/div/input"}   
(Session info: chrome=88.0.4324.150)

I have concluded the html should represent value=1234, however, I still am struggling with the code.


Solution

  • To send a character sequence to the element you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.find_element(By.CSS_SELECTOR, "input.[aria-label='Amount'][placeholder='0']").send_keys("1234")
      
    • Using XPATH:

      driver.find_element(By.XPATH, "//input[@aria-label='Amount' and @placeholder='0']").send_keys("1234")
      

    The desired element is a dynamic element, so ideally to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.[aria-label='Amount'][placeholder='0']"))).send_keys("1234")
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Amount' and @placeholder='0']"))).send_keys("1234")
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    References

    You can find a couple of relevant discussions on NoSuchElementException in: