我正在Selenium中运行一个案例,我要验证是否所有元素都显示在网页上,但仅希望自定义断言在测试结束时显示,如果找不到一个或多个。当前,使用driver.FindElement时,如果在标识时不将Element分配给变量,则无法克服ElementNotFound异常。有没有办法解决?这是我当前的代码
IWebElement userIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
userIcon.Click();
IWebElement profileIcon = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(1)"));
profileIcon.Click();
IWebElement headerLogo = driver.FindElement(By.CssSelector(".company-logo"));
IWebElement headerMsgDrop = driver.FindElement(By.CssSelector(".fa-envelope-o"));
headerMsgDrop.Click();
IWebElement headerMsgDropSubGeneral = driver.FindElement(By.CssSelector("li.dropdown:nth-child(2) > ul:nth-child(2)"));
Assert.Multiple(() =>
{
Assert.IsTrue(headerLogo.Displayed, "Header Logo NOT Present");
Assert.IsTrue(headerMsgDrop.Displayed, "Header Main Message Drop NOT Present");
Assert.IsTrue(headerMsgDropSubGeneral.Displayed, "Header - Message - General Option NOT Present");
});
IWebElement headerUserDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > a:nth-child(1)"));
headerUserDrop.Click();
IWebElement headerUserDropSubProfile = driver.FindElement(By.CssSelector(".user-name"));
IWebElement headerUserDropSubCredentials = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerUserDropSubSettings = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));
IWebElement headerUserDropSubChgPass = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)"));
IWebElement headerUserDropSubRstGrid = driver.FindElement(By.CssSelector("#clearLocalStorage"));
IWebElement headerUserDropSubLogOff = driver.FindElement(By.CssSelector("li.dropdown:nth-child(3) > ul:nth-child(2) > li:nth-child(6) > a:nth-child(1)"));
Assert.Multiple(() =>
{
Assert.IsTrue(headerUserDrop.Displayed, "Header Main User Drop NOT Present");
Assert.IsTrue(headerUserDropSubProfile.Displayed, "Header - User - Profile Option NOT Present");
Assert.IsTrue(headerUserDropSubCredentials.Displayed, "Header - User - Credentials Option NOT Present");
Assert.IsTrue(headerUserDropSubSettings.Displayed, "Header - User - Settings Option NOT Present");
Assert.IsTrue(headerUserDropSubChgPass.Displayed, "Header - User - Change Password Option NOT Present");
Assert.IsTrue(headerUserDropSubRstGrid.Displayed, "Header - User - Reset Grid Option NOT Present");
Assert.IsTrue(headerUserDropSubLogOff.Displayed, "Header - User - Log off Option NOT Present");
});
IWebElement headerSupportDrop = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > a:nth-child(1)"));
headerSupportDrop.Click();
IWebElement headerSupportDropSubBase = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(1) > a:nth-child(1)"));
IWebElement headerSupportDropSubFaq = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)"));
IWebElement headerSupportDropSubTicket = driver.FindElement(By.CssSelector("li.dropdown:nth-child(4) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)"));
Assert.Multiple(() =>
{
Assert.IsTrue(headerSupportDrop.Displayed, "Header Main Support Drop NOT Present");
Assert.IsTrue(headerSupportDropSubBase.Displayed, "Header - Support - Knowledge Base Option NOT Present");
Assert.IsTrue(headerSupportDropSubFaq.Displayed, "Header - Support - FAQ Option NOT Present");
Assert.IsTrue(headerSupportDropSubTicket.Displayed, "Header - Support - Submit Request Option NOT Present");
});
IWebElement emailTextInputField = driver.FindElement(By.CssSelector("div.col-md-10:nth-child(2)"));
IWebElement saveBut = driver.FindElement(By.CssSelector(".btn"));
IWebElement body = driver.FindElement(By.TagName("body"));
Assert.Multiple(() =>
{
Assert.IsTrue(body.Text.Contains("Profile"),"Profile Text NOT Present");
Assert.IsTrue(body.Text.Contains("Email"),"Email Text NOT Present");
Assert.IsTrue(body.Text.Contains("© 2018 - Stage Front Tickets"),"Copyright Text NOT Present");
Assert.IsTrue(emailTextInputField.Displayed, "Email Text Input Field NOT Present");
Assert.IsTrue(saveBut.Displayed, "Save Button NOT Present");
});
driver.Close();
}
答案 0 :(得分:0)
您可能正在寻找这种解决方案:
public IWebElement Get(By byLocator, double seconds = 10)
{
IWebElement element = null;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
try
{
element = wait.Until(ExpectedConditions.ElementExists(byLocator));
}
catch (Exception) { }
return element;
}
或者您也可以等待它被点击。
ExpectedConditions.ElementToBeClickable(byLocator)
答案 1 :(得分:0)
在捕获下一个元素并对其执行某些操作之前,您似乎没有在等待或者没有给UI正确加载。
执行下一步之前,您需要添加一些等待时间。 例如。 waitTillElementPresent