等待显示两个元素之一
因此,我在工作中自动化的这个应用程序具有场景,其中使用相同的按钮根据可能发生的各种场景导航到不同的屏幕。该应用程序还存在各种定时问题,通常在自动化过程中弹出。
以前,我已经使用WebDriverWait
通过等待显示/不显示元素来处理这些计时问题显示。
我找到了一种使用xpath
进行操作的方法,我使用管道字符本质上结合了2个XPath:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(maxTimeoutInSeconds))
.Until(ExpectedConditions.ElementIsVisible(By.XPath("//select[@id='dropdown']|//input[@id='firstname']")));
有没有一种方法可以用xpath
之外的其他方法来完成此操作by.id
或by.linktext
?
是否有更推荐的方法来处理element1 或元素的等待?
更新
我将基于下面评论的方法扔了在一起,并且每当显示的第一个条件是错误的,并且从未真正到达第二名时,它会抛出nosuchelementException
。
public void WaitForStuff(By element1, By element2)
{
try
{
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(driver => driver.FindElement(element1).Displayed || driver.FindElement(element2).Displayed);
}
catch (Exception e)
{
throw;
}
}
So this app I'm automating at work has scenarios where the same button is used to navigate to different screens based on various scenarios that could happen. The app also has various timing issues that usually pop up during automation.
Previously I've used WebDriverWait
for handling these timing issues by waiting for an element to be displayed / not displayed but now I find myself having to wait for element1 or element2 to be displayed.
I found a way of doing it with an XPath
where I use the pipe character to essentially combine 2 xpaths:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(maxTimeoutInSeconds))
.Until(ExpectedConditions.ElementIsVisible(By.XPath("//select[@id='dropdown']|//input[@id='firstname']")));
Is there a way to do this with something other than XPath
, like By.Id
or By.LinkText
?
Is there a more recommended approach for handling a wait for element1 or element2?
UPDATE
I threw together a method based on a comment below and it's throwing a NoSuchElementException
whenever the first Displayed
condition is false and never actually making it to the 2nd.
public void WaitForStuff(By element1, By element2)
{
try
{
new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(driver => driver.FindElement(element1).Displayed || driver.FindElement(element2).Displayed);
}
catch (Exception e)
{
throw;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我高度强调
它在此行停止,直到显示两个元素之一。这对我来说是可靠的。我用无数次使用它,很少让我失望。
编辑
查看您的更新,这是一个答案,它考虑了未存在的元素。
通过寻找多个元素,它找不到元素,而是返回一个空列表,如果列表为空,它将不会继续通过和语句,这意味着它不会尝试查看是否显示元素,除非存在。
I highly reccommend using this
It stops at this line until one of the two elements is displayed. It's an ole' reliable for me. I use it countless times and it's rarely let me down.
Edit
Seeing your update, here is an answer that takes into account the elements not existing.
By looking for multiple elements, it won't throw element not found, but return an empty list, and if the list is empty it won't continue through the and statement, meaning it won't try to see if an element is displayed, unless it exists.