首页 文章

无法找到元素:org.openqa.selenium.NoSuchElementException

提问于
浏览
0

我正在用一次性密码(OTP)编写手机号码验证脚本 . 当OTP弹出窗口打开时,我无法在文本字段中输入值,系统显示错误:

org.openqa.selenium.NoSuchElementException:无法找到元素:{“method”:“class name”,“selector”:“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

2 回答

  • 0

    您需要更正“OTP文本字段”的Xpath,如下所示 .

    driver.findElement(By.xpath("//*[@id='verifyOTP_register']//*[@id='otp']")).sendKeys("1212121212");
    

    此外,您可以使用“relative”xpath作为“提交”按钮,而不是使用“绝对”xpath .

    driver.findElement(By.xpath("//*[@id='verifyOTP_register']//*[@type='submit']")).click();
    
  • 0
    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等价物 . 或者,您可以使用该元素唯一的不同定位器 .

相关问题