2017-08-01 71 views
2

代碼到達「幾乎結束」之前的行時拋出錯誤。 但引發錯誤的行org.openqa.selenium.NoSuchElementException:沒有這樣的元素使用TestNG

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) 
+1

你確定你的頁面上有一個id爲「txtEmail」的元素? – bert

回答

2

我認爲已經取得了非常愚蠢的錯誤..

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

你應該已經結束for循環打印Cookie列表之後。試試這個

for(Cookie getcookies :cookiesList) { 
       System.out.println(getcookies); 
} 
... 
.. 
相關問題