等待显示两个元素之一

发布于 2025-02-08 08:58:26 字数 1034 浏览 7 评论 0原文

因此,我在工作中自动化的这个应用程序具有场景,其中使用相同的按钮根据可能发生的各种场景导航到不同的屏幕。该应用程序还存在各种定时问题,通常在自动化过程中弹出。

以前,我已经使用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.idby.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2025-02-15 08:58:26

我高度强调

wait.Until(driver => driver.FindElement(By.XPath("xpath")).Displayed
                  || driver.FindElement(By.XPath("xpath")).Displayed);

它在此行停止,直到显示两个元素之一。这对我来说是可靠的。我用无数次使用它,很少让我失望。

编辑

查看您的更新,这是一个答案,它考虑了未存在的元素。

Driver.Wait.Until(driver => (driver.FindElements(By.XPath("locationOne")).Any() && driver.FindElement(By.XPath("locationOne")).Displayed)
                         || (driver.FindElements(By.XPath("locationTwo")).Any() && driver.FindElement(By.XPath("locationTwo")).Displayed));

通过寻找多个元素,它找不到元素,而是返回一个空列表,如果列表为空,它将不会继续通过和语句,这意味着它不会尝试查看是否显示元素,除非存在。

I highly reccommend using this

wait.Until(driver => driver.FindElement(By.XPath("xpath")).Displayed
                  || driver.FindElement(By.XPath("xpath")).Displayed);

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.

Driver.Wait.Until(driver => (driver.FindElements(By.XPath("locationOne")).Any() && driver.FindElement(By.XPath("locationOne")).Displayed)
                         || (driver.FindElements(By.XPath("locationTwo")).Any() && driver.FindElement(By.XPath("locationTwo")).Displayed));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文