线程“main"中的异常org.openqa.selenium.NoSuchElementException:无法定位元素://*[@id='login-email'] [英] Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='login-email']

查看:32
本文介绍了线程“main"中的异常org.openqa.selenium.NoSuchElementException:无法定位元素://*[@id='login-email']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不重新测试 xpath,以前它工作正常,但现在它给了我一个错误.

I had to re-test the xpath, Previously it was working fine, But now it gives me an error.

我也尝试了不同的定位器,比如idname.但仍然得到同样的错误.

I tried with different locators as well, Like id, name. but still get the same error.

package staging;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class login {

    public static void main (String[]args){
        System.setProperty("webdriver.gecko.driver","C:\Program Files\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();

        //opening the browser
        driver.get("https://staging.keela.co/login");

        //logging
        driver.findElement(By.xpath("//*[@id='login-email']")).sendKeys("bandanakeela@yopmail.com");
        driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela");
        driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();       
 }
}

推荐答案

当您访问 url https://staging.keela.co/login 时,有一个 Ajax 加载器会阻止 UI,所以我们必须等待 Ajax 加载器完成加载所有 WebElements 并且 emailpassword 字段变得可见.为了实现这一点,我们将引入 ExplicitWait ,即 WebDriverWait 并将 ExpectedConditions 设置为 emailelementToBeClickable代码> 字段.这是工作代码块:

As you access the url https://staging.keela.co/login there is a Ajax loader which blocks the UI, so we have to wait for the Ajax loader to complete loading the all the WebElements and the email and password field becomes visible. To achieve that we will introduce ExplicitWait i.e. WebDriverWait with ExpectedConditions set to elementToBeClickable for the email field.Here is the working code block:

System.setProperty("webdriver.gecko.driver","C:\Utility\BrowserDrivers\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://staging.keela.co/login");
WebDriverWait wait = new WebDriverWait (driver, 15);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='login-email']")));
element.sendKeys("bandanakeela@yopmail.com");
driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click(); 

这篇关于线程“main"中的异常org.openqa.selenium.NoSuchElementException:无法定位元素://*[@id='login-email']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆