Ответ 1
Я искал альтернативы, и я решил использовать следующие версии. Все используют явное ожидание с заданным таймаутом и основаны на свойствах элемента в первом случае и на стойкости элемента во втором случае.
Первый выбор будет проверять свойства элемента до тех пор, пока не будет достигнут тайм-аут. Я пришел к следующим свойствам, подтверждающим, что он доступен на странице:
Существование. Ожидание проверки того, что элемент присутствует в DOM страницы. Это не обязательно означает, что элемент виден.
//this will not wait for page to load
Assert.True(Driver.FindElement(By elementLocator).Enabled)
//this will search for the element until a timeout is reached
public static IWebElement WaitUntilElementExists(By elementLocator, int timeout = 10)
{
try
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
return wait.Until(ExpectedConditions.ElementExists(elementLocator));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}
Видимость. Ожидание проверки того, что элемент присутствует в DOM страницы и видимый. Видимость означает, что элемент не только отображается, но также имеет высоту и ширину, превышающую 0.
//this will not wait for page to load
Assert.True(Driver.FindElement(By elementLocator).Displayed)
//this will search for the element until a timeout is reached
public static IWebElement WaitUntilElementVisible(By elementLocator, int timeout = 10)
{
try
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
return wait.Until(ExpectedConditions.ElementIsVisible(elementLocator));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found.");
throw;
}
}
Clickable. Ожидание проверки элемента видимо и включено так, что вы можете щелкнуть его.
//this will not wait for page to load
//both properties need to be true in order for element to be clickable
Assert.True(Driver.FindElement(By elementLocator).Enabled)
Assert.True(Driver.FindElement(By elementLocator).Displayed)
//this will search for the element until a timeout is reached
public static IWebElement WaitUntilElementClickable(By elementLocator, int timeout = 10)
{
try
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
return wait.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}
Второй выбор применяется, когда объект триггера, например элемент меню, больше не привязан к DOM после его нажатия. Это обычно происходит, когда действие click на элементе вызывает перенаправление на другую страницу. В этом случае полезно проверить StalenessOf (element), где элемент - это элемент, который был нажат, чтобы вызвать перенаправление на новую страницу.
public static void ClickAndWaitForPageToLoad(By elementLocator, int timeout = 10)
{
try
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
var element = Driver.FindElement(elementLocator);
element.Click();
wait.Until(ExpectedConditions.StalenessOf(element));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}