0

在我的脚本中,我正在添加约会,并且在约会保存在屏幕顶部后,工具提示会出现一个文本“已成功保存”。我正在断言此文本,我尝试过,Thread.sleep(3000);但我的脚本有时会成功,但有时并非总是如此。现在我想为这个工具提示元素使用 webdriverwait,它目前没有出现在 DOM 中,但是当约会保存时它会出现一次。

4

2 回答 2

0

您可以使用 WebDriverWait 类,例如。

new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(locator));

这只是一个例子。有关更多信息,请查看http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html

于 2013-09-25T09:02:02.607 回答
0

您可以使用 WebDriverWait,这是 Selenium 中“等待”某事发生的标准方式,例如标题更改为特定文本、元素消失等。

对于您的特定情况,我通常定义以下方法来检测标签是否包含文档正文中存在的特定文本:

private static true IsElementExistsWithText(string text, string tagName, IWebDriver driver)
        {
            try
            {
                var result = driver.FindElement(By.CssSelector(string.Format("{0}:contains(\"{1}\")", tagName, text)));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

然后在我的代码中:

new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until((d) => { return IsElementExistsWithText("Saved Successfully", "span", driver); });
于 2013-09-25T12:53:37.520 回答