Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
org.openqa.selenium.NoSuchElementException:没有这样的元素:无法使用TestNG在selenium、java中找到元素_Java_Selenium_Testng - Fatal编程技术网

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法使用TestNG在selenium、java中找到元素

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法使用TestNG在selenium、java中找到元素,java,selenium,testng,Java,Selenium,Testng,我有一个场景,页面将我带到一个屏幕,其中有一条消息,如功能未启用。因此你在这里。 然后我需要导航到设置页面并启用它&重新加载表单以验证web元素 最初一切正常,第二次运行代码时。我没有遇到这样的元素异常。 因为该设置最初是禁用的,已经通过自动脚本启用,现在已启用,我可以看到表单元素。我必须只在找到元素时执行系统设置代码,否则我必须跳过代码 我尝试了下面的方法,但仍然没有发现这样的元素错误 if(driver.findElement(By.id("ViewErrorMessage")) != nu

我有一个场景,页面将我带到一个屏幕,其中有一条消息,如功能未启用。因此你在这里。 然后我需要导航到设置页面并启用它&重新加载表单以验证web元素

最初一切正常,第二次运行代码时。我没有遇到这样的元素异常。 因为该设置最初是禁用的,已经通过自动脚本启用,现在已启用,我可以看到表单元素。我必须只在找到元素时执行系统设置代码,否则我必须跳过代码

我尝试了下面的方法,但仍然没有发现这样的元素错误

if(driver.findElement(By.id("ViewErrorMessage")) != null) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}
在if条件下报告错误。
当找不到元素时,我假设它必须执行else循环,但它总是在同一点失败。任何人都可以提供帮助。

当驱动程序无法找到元素时,它抛出NoTouchElementFound异常。您可以将findElement包装在try-catch中以查看元素是否存在,也可以使用isEmpty方法

boolean isElementPresent=true;
try
{
   driver.findElement(By.id("ViewErrorMessage"));
}
catch
{
  isElementPresent=false;
}

if(isElementPresent) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}
或者你也可以这样做

if(!driver.findElements(By.id("ViewErrorMessage")).isEmpty()) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}

请尝试在if contion中显示,希望这将有助于:

if(driver.findElement(By.id("ViewErrorMessage").isDisplayed()) {

      String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
  Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

      //system setting code

      driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
      Thread.sleep(2000);
      driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
}else{

  //code to verify form elements goes here..
}
或者在下面使用

String s1 = "Error message";
String s2 = driver.findElement(By.id("ViewErrorMessage").getText());
if(s1 == s2) {

          String errorMsg = driver.findElement(By.xpath("//*[@id=\"ctl00_pnlViewErrorMessage\"]")).getText();
      Assert.assertEquals(errorMsg, "Form is disabled, Hence you are here. Enable the form");

          //system setting code

          driver.findElement(By.xpath("//input[@name='btnSignin']")).click();
          Thread.sleep(2000);
          driver.findElement(By.xpath("//img[@id='imgLogo']")).isDisplayed();
    }else{

      //code to verify form elements goes here..
    }

选项1对我有用,谢谢。但是选项2不起作用。isEmpty方法未在WebElement的可用方法中列出感谢您的时间Mahesh,我尝试了两种方法,但错误仍然存在:NoTouchElementException