C# WebDriverWait 如何等到项目存在或不存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19573943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebDriverWait how to wait until an item exists or doesn't exist?
提问by Rochelle C
I am running a test using Selenium WebDriver and if a user doesn't have access rights a div does not exist on the page. I am trying to do a wait so that if the item is displayed it returns true but if it hits the timeout it returns false.
我正在使用 Selenium WebDriver 运行测试,如果用户没有访问权限,则页面上不存在 div。我正在尝试等待,以便如果显示该项目,则返回 true,但如果超时,则返回 false。
public bool SummaryDisplayed()
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
var myElement = wait.Until(x => x.FindElement(By.Id("summaryPage")));
return myElement.Displayed;
}
I don't want to use Thread.Sleep because if the element is there after 2 seconds I want it to proceed. But if the element isn't there after 5 seconds it should return false. I don't want it to throw an exception, in some test cases I am expecting it to not exist. Is there a way I can suppress the exception and return false after the timeout? Thanks
我不想使用 Thread.Sleep 因为如果元素在 2 秒后存在,我希望它继续。但是如果元素在 5 秒后不存在,它应该返回 false。我不希望它抛出异常,在某些测试用例中,我希望它不存在。有没有办法可以抑制异常并在超时后返回 false?谢谢
采纳答案by Richard
This will work for you, I think.
我想这对你有用。
public bool SummaryDisplayed()
{
try
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
var myElement = wait.Until(x => x.FindElement(By.Id("summaryPage")));
return myElement.Displayed;
}
catch
{
return false;
}
}
回答by L202
Try using Autoreset events in system.Threading.
尝试在 system.Threading 中使用 Autoreset 事件。
public static AutoResetEvent messsageEvent= new AutoResetEvent(false);
Public bool summaryDisplay()
{
var value=SomemethodCall()
messsageEvent.Reset()//This blocks the Thread
if(value.Exists)
{
messageEvent.Set();//This releases the Thread
}
}
This is one more method messageEvent.WaitOne(millisecond, Bool value);
这是另一种方法 messageEvent.WaitOne(millisecond, Bool value);
I think this wil help you
我认为这会帮助你
回答by Faiz
You can use a generic extension method to wait for an element to be visible within a specified wait; and then just call it from your methods as below:
您可以使用通用扩展方法来等待元素在指定的等待时间内可见;然后只需从您的方法中调用它,如下所示:
public bool SummaryDisplayed()
{
var summaryElement = driver.WaitGetElement(By.Id("summaryPage"), 5, true);
return (summaryElement !=null);
}
The extension method simply wraps the WebDriverWait
and waits for the element to exist/be displayed, depending on what you asked for. If element is not found within the specified wait, it returns null
. I use this extension instead of FindElement()
in my frameworks.
扩展方法简单地包装WebDriverWait
并等待元素存在/显示,具体取决于您的要求。如果在指定的等待中未找到元素,则返回null
。我使用这个扩展而不是FindElement()
在我的框架中。
public static IWebElement WaitGetElement(this IWebDriver driver, By by, int timeoutInSeconds, bool checkIsVisible=false)
{
IWebElement element;
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
try
{
if (checkIsVisible)
{
element = wait.Until(ExpectedConditions.ElementIsVisible(by));
}
else
{
element = wait.Until(ExpectedConditions.ElementExists(by));
}
}
catch (NoSuchElementException) { element = null; }
catch (WebDriverTimeoutException) { element = null; }
catch (TimeoutException) { element = null; }
return element;
}