![](/img/trans.png)
[英]org.openqa.selenium.NoSuchElementException: Unable to locate element error
[英]Unable to locate element: org.openqa.selenium.NoSuchElementException
我正在編寫用於通過一次性密碼(OTP)進行手機號碼驗證的腳本。 在打開OTP彈出窗口時,我無法在文本字段中輸入值,系統顯示錯誤:
org.openqa.selenium.NoSuchElementException:無法找到元素:{“方法”:“類名”,“選擇器”:“ opt_success”}命令持續時間或超時:30.04秒”
下面是我起草的代碼。
driver.findElement(By.id("phone")).sendKeys(Constants.MOBILE_NUMBER);
driver.findElement(By.id("btn_verify")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement otp_value = driver.findElement(By.id("otp"));
otp_value.sendKeys("1212121212");
driver.findElement(By.xpath("html/body/div[4]/div/form/div/div[4]/span[1]/input")).click();
網頁網址為: http : //talentrack.in/register
WebElement otp_value = driver.findElement(By.id("otp"));
這是你的問題。 在頁面上,有2個元素的ID為“ otp”。 您正在找到第一個隱藏的,但您需要第二個。
您可以使用WebDriverWait查找可見元素。 我是這樣用Python完成的:
element = WebDriverWait(driver, 0).until(EC.presence_of_element_located((By.ID, "otp")))
return element
傳遞給WebDriverWait的0超時意味着它只會嘗試一次查找元素。 您可以創建一個執行此操作的方法,然后將其傳遞給超時參數以方便重用。
我確定有Java等效項。 或者,您可以使用元素唯一的其他定位器。
您需要按如下所示為“ OTP文本字段”更正Xpath。
driver.findElement(By.xpath("//*[@id='verifyOTP_register']//*[@id='otp']")).sendKeys("1212121212");
另外,您可以將“相對” xpath用於“提交”按鈕,而不是使用“絕對” xpath。
driver.findElement(By.xpath("//*[@id='verifyOTP_register']//*[@type='submit']")).click();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.