2017-05-28 34 views
1

在執行我的測試腳本時觀察以下錯誤。有人能幫助我找出失敗的原因嗎?NoSuchElementException當執行我的測試腳本時

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"lid"} 
Command duration or timeout: 20.45 seconds 

這是我的代碼的片段。請注意,元素存在於同一幀中,因此不需要幀交換。元素的

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
//import org.openqa.selenium.support.ui.ExpectedConditions; 
//import org.openqa.selenium.support.ui.WebDriverWait; 
import org.testng.annotations.Test; 

public class LoginPage 
{ 
    @Test 
    public void testLoginFail() 
    { 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("https://www.zoho.com/crm/"); 
     driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); 
     driver.findElement(By.linkText("LOGIN")).click(); 
     //WebDriverWait wait = new WebDriverWait(driver,40); 
     //wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("lid")))); 
     driver.findElement(By.id("lid")).sendKeys("[email protected]"); 

HTML的觀點是:

<input name="lid" id="lid" class="input usrbx" value="" onkeypress="clearmsg()" type="email"> 
+1

你試過包裝'driver.findElement(By.id(「lid」))。sendKeys(「[email protected]」);''顯式等待'中的語句嗎?另外附上登錄表單HTML,人們將很難幫助你。我會做的是打開瀏覽器控制檯,並嘗試查看用'.val()'填充輸入的選擇器。 (例如''('#lid')。val(「[email protected]」)')。如果它與控制檯JQuery協同工作,它將與你的選擇器一起工作。 :) – iamdanchiv

+0

我已經嘗試用明確的等待包裝這個語句,但它沒有奏效。 – Dks

+0

@Dks你有沒有試過我的解決方案? –

回答

0

我已檢查了網頁「https://www.zoho.com/crm/lp/login.html」,我能看到一個iframe在它和輸入文本框裏面。以下是工作代碼。

public static void main(String[] args) 
{ 

    System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\Selenium\\Web Drivers\\chromedriver.exe"); 
    WebDriver driver=new ChromeDriver(); 


    driver.get("https://www.zoho.com/crm/"); 
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); 
    driver.findElement(By.linkText("LOGIN")).click(); 

    //Switch to the frame 
    driver.switchTo().frame(0); 
    driver.findElement(By.id("lid")).sendKeys("[email protected]"); 
    driver.quit(); 
} 

希望這可以幫助你。謝謝。

+0

是的,這段代碼適合我。 – Dks

+0

感謝您的支持。 – Dks

0

這裏是回答你的問題:

有關解決方案的幾句話:

  1. 雖然硒3.4.0正與geckodriver v0.16.1 &是Mozilla Firefox 53.0您需要下載從here最新的geckodriver,並通過System.setProperty提供geckodriver的絕對路徑。
  2. 儘量避免implicitlyWait根據最近更新implicitlyWait可能會很快死亡。
  3. 您正在查看的錯誤NoSuchElementException這樣說。元素的id不可追蹤。
  4. ID爲lid的元素位於iframe中,因此您需要先切換到幀。
  5. 這是你自己的代碼中有一些簡單的調整:

    @Test 
    public void testLoginFail() 
    { 
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); 
        WebDriver driver = new FirefoxDriver(); 
        driver.get("https://www.zoho.com/crm/"); 
        driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); 
        driver.findElement(By.linkText("LOGIN")).click(); 
        driver.switchTo().frame("zohoiam"); 
        driver.findElement(By.xpath("//input[@id='lid']")).sendKeys("[email protected]"); 
    
    
    } 
    

讓我知道如果這個回答你的問題。

+0

是的,我用我的代碼切換到框架,現在它工作。謝謝 – Dks

+0

@Dks聽起來不錯!如果我的答案滿足了您的問題,您是否可以接受答案作爲解決方案?謝謝 – DebanjanB

相關問題