简体   繁体   中英

selenium.common.exceptions.NoSuchElementException: Message:

I'm trying to write a simple program to fill out a form (including order ID and zip code) to be submitted but I keep getting the following error: "selenium.common.exceptions.NoSuchElementException: Message: " (without any text following "Message").

from selenium import webdriver
browser = webdriver.Safari()
browser.get('https://knowledge.tonal.com/s/order-status')

orderElm = browser.find_element_by_id('input-3')
orderElm.send_keys('1000XXX')

zipcodeElm = browser.find_element_by_id('input-4')
zipcodeElm.send_keys('90210')
zipcodeElm.submit()

I've double-checked my element ID several times and though I'm very new to this, I'm fairly confident I have the correct element IDs. What am I doing incorrectly? Thanks in advance!

There are mutliple issues here: A. After getting the url, you are not waiting for the elements in the page to load completely and hence the elements are not found B. The locators you have seem dynamic to me, for eg: input-3 , I see it as input-5 (although I am on Chrome browser, but that may not rule out that the locators are dynamic). So, I refactored and hunted for some static locator strategies which I pasted below. C. zipcodeElm.submit() would not work, as it is not the button element. I have refactored this too. So, here is the code.

driver.get('https://knowledge.tonal.com/s/order-status')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//div[@data-aura-class='cOrderSearch']//input)[1]")))
orderElm = driver.find_element(By.XPATH, "(//div[@data-aura-class='cOrderSearch']//input)[1]")
orderElm.send_keys('1000XXX')
zipcodeElm = driver.find_element(By.XPATH, "(//div[@data-aura-class='cOrderSearch']//input)[2]")
zipcodeElm.send_keys('90210')
driver.find_element(By.XPATH, "//*[@data-aura-class='cOrderSearch']//parent::div//button").click()

Output: (exit code 0 implies that the code passed without errors)

Process finished with exit code 0

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 XPATH :

     driver.get("https://knowledge.tonal.com/s/order-status") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-aura-class='cOrderSearch']//following::input[1]"))).send_keys('1000XXX') driver.find_element(By.XPATH, "//div[@data-aura-class='cOrderSearch']//following::input[2]").send_keys("90210")
  • 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
  • Browser Snapshot:

tonal_com

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