当代码到达“几乎结束”之前的行时,抛出错误。 但是行
会引发错误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");
}
}
错误
FAILED: rexulti
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"txtEmail"}
(Session info: chrome=59.0.3071.115)
答案 0 :(得分:2)
我认为已经犯了一个非常愚蠢的错误。
for(Cookie getcookies :cookiesList) {
System.out.println(getcookies );
...
...
}
您应该在打印Cookie列表后结束for循环。试试这个
for(Cookie getcookies :cookiesList) {
System.out.println(getcookies );
}
...
..