2016-08-04 138 views
2

我使用Selenium來查找元素並使用文本填充它。當我瀏覽我的網址時,一個文本框首先顯示加載頁面的內容。應用程序加載完成後,會顯示一個登錄用戶名/密碼。我只想等到顯示登錄用戶名/密碼以避免NoSuchElementException。我引用了this post,但無法獲得任何解決方案。NoSuchElementException儘管使用WebDriverWait

當我運行測試時,它不會等待元素出現,並立即在x.FindElement(By.Id("UserName")上失敗,並顯示NoSuchElementException

using (IWebDriver driver = new ChromeDriver(@"<my path>")) 
{ 
    driver.Manage().Window.Maximize(); 

    driver.Navigate().GoToUrl("<my url>"); 

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(600)); 
    var userName = wait.Until(x => x.FindElement(By.Id("UserName"))); 

    userName.SendKeys("username"); 

    IWebElement pass = driver.FindElement(By.Id("Password")); 
    pass.SendKeys("password"); 
} 

我在做什麼錯在這裏?我應該如何更改我的代碼以便在查找之前等待UserName ID?

回答

2

我認爲你需要等待加載元件的第一隱形然後去等待下面的用戶名的可見性: -

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20)); 

wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.ID("id of loading element "))); 

//Now go to find username element 
var userName = wait.Until(ExpectedConditions.ElementIsVisible(By.ID("UserName"))); 
userName.SendKeys("username"); 
+1

謝謝!我結束了只是使用'wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id(「UserName」)));'那工作。 – tnw

相關問題