Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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
Java org.openqa.selenium.NoSuchElementException:没有使用TestNG的元素_Java_Selenium_Testng_Testng Eclipse - Fatal编程技术网

Java org.openqa.selenium.NoSuchElementException:没有使用TestNG的元素

Java org.openqa.selenium.NoSuchElementException:没有使用TestNG的元素,java,selenium,testng,testng-eclipse,Java,Selenium,Testng,Testng Eclipse,当代码到达“几乎结束”之前的行时抛出错误。 但是该行会抛出错误 driver.findElement(By.id("txtEmail")).sendKeys("abc.in"); driver.findElement(By.id("btnEmailSubmit")).click(); 请帮助我找出导致此异常的原因 注意:使用TestNG时会显示错误 代码 import org.testng.annotations.Test; import java.io.IOException; import

当代码到达“几乎结束”之前的行时抛出错误。 但是该行会抛出错误

driver.findElement(By.id("txtEmail")).sendKeys("abc.in");
driver.findElement(By.id("btnEmailSubmit")).click();
请帮助我找出导致此异常的原因

注意:使用TestNG时会显示错误

代码

import org.testng.annotations.Test;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Otsuka {
  @Test
public void rexulti() throws InterruptedException {
      System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();

      driver.get("https://abc/Login.aspx");
        //Setcookie.cookie(driver);
      Cookie name = new Cookie("y1LTtty8PiKwyrj/S5kKv6arXj2KsuVDm5OtdDIUnOQ#","Token=40287A4F0EA6");
        driver.manage().addCookie(name);
        Set<Cookie> cookiesList =  driver.manage().getCookies();
        for(Cookie getcookies :cookiesList) {
            System.out.println(getcookies );

        driver.findElement(By.id("txtEmail")).sendKeys("abc.in");
        driver.findElement(By.id("btnEmailSubmit")).click();

        Thread.sleep(1000);
        driver.findElement(By.cssSelector(".select2-choice.select2-default>span:nth-child(3)")).click();

        Thread.sleep(4000);

         driver.findElement(By.cssSelector("#select2-results-1>li:nth-child(1)>div")).click();

        List<WebElement> allSuggestions = driver.findElements(By.cssSelector("#select2-results-1>li"));      
      for (WebElement suggestion : allSuggestions)
   {
        if(suggestion.getText().equals("ALASKA AK (OCRGA061A) - HCP")) {
            suggestion.click();
            break;
        }

   }
        Thread.sleep(4000);
        driver.findElement(By.cssSelector(".select2-choice.select2-default>span:nth-child(3)")).click();
        Thread.sleep(4000);

        List<WebElement> allSuggestionsprovider = driver.findElements(By.cssSelector("#select2-results-1>li"));      
          for (WebElement suggestionprovider : allSuggestionsprovider)
       {
            if(suggestionprovider.getText().equals("ABRAM, JILL")) {
                suggestionprovider.click();
                break;
            }
        }

    System.out.println("almost end");
  }
}

我认为已经犯了一个非常愚蠢的错误

for(Cookie getcookies :cookiesList) {
            System.out.println(getcookies );
...
...
}
您应该在打印cookie列表后结束for循环。试试这个

for(Cookie getcookies :cookiesList) {
                System.out.println(getcookies );
}
...
..

您确定页面上有id为“txtEmail”的元素吗?
for(Cookie getcookies :cookiesList) {
                System.out.println(getcookies );
}
...
..