等待 webdriver 中的元素

发布于 12-08 16:32 字数 855 浏览 0 评论 0原文

我按照这里写的: WebDriver Selenium API:当 Element 明显存在时出现 ElementNotFoundErrorException!

我的代码看起来像:

    Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
    return new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
          }
       };
    }

   ....... 

   driver.get(baseUrl);

   WebDriverWait wait = new WebDriverWait(driver, 10);
   wait.until(presenceOfElementLocated(By.className("classname")));
   findByClassAndName(driver, "x-tree3-node-text", "Name1").click();

问题是,那似乎没有做任何事情。它不起作用,我什至看不到一丝等待网页 GUI 的痕迹。我通过超时隐式等待也得到了同样的结果...任何人都可以帮忙吗?

I followed what was written here:
WebDriver Selenium API: ElementNotFoundErrorException when Element is clearly there !

My code looks like:

    Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
    return new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
          }
       };
    }

   ....... 

   driver.get(baseUrl);

   WebDriverWait wait = new WebDriverWait(driver, 10);
   wait.until(presenceOfElementLocated(By.className("classname")));
   findByClassAndName(driver, "x-tree3-node-text", "Name1").click();

Problem is, that is does not seem to do anything. It doesn't work and i can't even see slightest trace of waiting for webpage gui. i got the same with implicit wait through timeouts... Anyone could help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你对谁都笑2024-12-15 16:32:33

您必须捕获从 ExpectedCondition 或 Function 抛出的 Throwables(apply() 方法是一个很好的地方)并返回 null,以便 Wait.until() 继续运行 -请参阅http://rostislav-matl.blogspot.com/2011/05/moving-to-selenium-2-on-webdriver-part.html 了解详细示例。

You have to catch Throwables throwed from ExpectedCondition or your Function (the apply() methods is good place for that) and return null so Wait.until() continues to run - see http://rostislav-matl.blogspot.com/2011/05/moving-to-selenium-2-on-webdriver-part.html for detailed example.

一身软味2024-12-15 16:32:33

创建函数如下:

public void Wait (string element)          // Wait function to wait for element
        { 
            for (int second = 0; ; second++)
                {
                    if (second >= 60) Assert.Fail("timeout");
                    try
                    {
                        if (IsElementPresent(By.LinkText(element))) break;
                    }
                    catch (Exception)
                    { }
                    Thread.Sleep(1000);
                 }  
        }

现在在您想要等待元素的地方调用此函数,如下所示:

string element="<element name>";
        Wait(element);

Create function as follows :

public void Wait (string element)          // Wait function to wait for element
        { 
            for (int second = 0; ; second++)
                {
                    if (second >= 60) Assert.Fail("timeout");
                    try
                    {
                        if (IsElementPresent(By.LinkText(element))) break;
                    }
                    catch (Exception)
                    { }
                    Thread.Sleep(1000);
                 }  
        }

and now call this function where you want to wait for element as follows:

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