요소가 존재하고 표시 될 때까지 기다리는 비교적 간단한 방법이 있습니다. 메서드는 주어진 By에 대해 하나 이상의 요소가 반환되는 상황을 처리합니다 (일반적으로 그중 하나만 표시 될 것으로 예상하지만 어떤 경우에도 메서드는 발견 된 첫 번째 표시된 요소를 반환합니다).
내가 가진 문제는 페이지에 일치하는 요소가 없을 때 (전혀) 지정된 TimeSpan보다 더 많은 시간이 걸리고 그 이유를 알 수 없다는 것입니다.
* 저는 30 초 타임 아웃으로 테스트했는데 5m 조금 넘었습니다.
암호:
/// <summary>
/// Returns the (first) element that is displayed when multiple elements are found on page for the same by
/// </summary>
public static IWebElement FindDisplayedElement(By by, int secondsToWait = 30)
{
WebDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(secondsToWait);
// Wait for an element to exist and also displayed
IWebElement element = null;
bool success = SpinWait.SpinUntil(() =>
{
var collection = WebDriver.FindElements(by);
if (collection.Count <= 0)
return false;
element = collection.ToList().FirstOrDefault(x => x.Displayed == true);
return element != null;
}
, TimeSpan.FromSeconds(secondsToWait));
if (success)
return element;
// if element still not found
throw new NoSuchElementException("Could not find visible element with by: " + by.ToString());
}
다음과 같이 호출합니다.
[Test]
public void FindDisplayedElement()
{
webDriver.Navigate().GoToUrl("https://stackoverflow.com/questions");
var nonExistenetElementBy = By.CssSelector("#custom-header99");
FindDisplayedElement(nonExistenetElementBy , 10);
}
테스트를 실행하면 (10 초 시간 초과) 실제로 종료하는 데 약 100 초가 소요됩니다.
SpinWait.WaitUntil () 내부에 래핑 된 WebDriver.FindElements ()에 내장 된 상속 대기의 혼합과 관련이있을 수 있습니다.
이 수수께끼에 대해 어떻게 생각하는지 듣고 싶습니다.
건배!
몇 가지 추가 테스트를 통해 WebDriver 암시 적 대기 시간 제한을 낮은 숫자 (예 : 100ms)로 줄이면 문제가 해결된다는 사실을 알게되었습니다. 이것은 SpinUntil 사용이 작동하지 않는 이유에 대해 @Evk가 제공 한 설명에 해당합니다 .
대신 WebDriverWait을 사용하도록 기능을 변경했으며 ( 다른 질문에 대한이 답변에 표시된대로 ) 이제 올바르게 작동합니다. 이로 인해 암시 적 대기 시간 제한을 사용할 필요가 전혀 없습니다.
/// <summary>
/// Returns the (first) element that is displayed when multiple elements are found on page for the same by
/// </summary>
/// <exception cref="NoSuchElementException">Thrown when either an element is not found or none of the found elements is displayed</exception>
public static IWebElement FindDisplayedElement(By by, int secondsToWait = DEFAULT_WAIT)
{
var wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(secondsToWait));
try
{
return wait.Until(condition =>
{
return WebDriver.FindElements(by).ToList().FirstOrDefault(x => x.Displayed == true);
});
}
catch (WebDriverTimeoutException ex)
{
throw new NoSuchElementException("Could not find visible element with by: " + by.ToString(), ex);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다