简体   繁体   中英

Selenium Python | Type in input

在此处输入图像描述

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM