org.openqa.selenium.NoSuchElementException: 没有这样的元素使用 TestNG
org.openqa.selenium.NoSuchElementException: no such element using TestNG
当代码到达 "almost end" 之前的行时抛出错误。
但是对于行
抛出错误
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)
我认为犯了一个非常愚蠢的错误..
for(Cookie getcookies :cookiesList) {
System.out.println(getcookies );
...
...
}
您应该在打印 cookie 列表后结束 for 循环。试试这个
for(Cookie getcookies :cookiesList) {
System.out.println(getcookies );
}
...
..
当代码到达 "almost end" 之前的行时抛出错误。 但是对于行
抛出错误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)
我认为犯了一个非常愚蠢的错误..
for(Cookie getcookies :cookiesList) {
System.out.println(getcookies );
...
...
}
您应该在打印 cookie 列表后结束 for 循环。试试这个
for(Cookie getcookies :cookiesList) {
System.out.println(getcookies );
}
...
..