[英]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").我正在尝试编写一个简单的程序来填写要提交的表单(包括订单 ID 和 zip 代码),但我不断收到以下错误:“selenium.common.exceptions.NoSuchElementException: 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.我已经多次仔细检查了我的元素 ID,虽然我对此很陌生,但我相当有信心我拥有正确的元素 ID。 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).这里有多个问题: A. 获得 url 后,您不会等待页面中的元素完全加载,因此找不到元素 B. 您的定位器对我来说似乎是动态的,例如:
input-3
,我将其视为input-5
(虽然我使用的是 Chrome 浏览器,但这可能不排除定位器是动态的)。 So, I refactored and hunted for some static locator strategies which I pasted below.因此,我重构并寻找了一些我粘贴在下面的 static 定位器策略。 C.
C。
zipcodeElm.submit()
would not work, as it is not the button element. zipcodeElm.submit()
不起作用,因为它不是按钮元素。 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) Output:(退出码0表示代码无误通过)
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 :要将字符序列发送到您需要为element_to_be_clickable()诱导WebDriverWait的元素,您可以使用以下任一Locator Strategies :
Using XPATH
:使用
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:浏览器快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.