模拟WebDriverWait仅适用于WebElement

时间:2016-07-28 16:21:52

标签: java selenium

我找到了使用下一个结构的元素。

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement wsearchlist = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));

之后我想寻找内部元素

By by = new By.ByLinkText(testName);
wsearch = wsearchlist.findElement(by);

此行为不是要等待内部元素的可用性。我想使用相同或相同的东西来等待"等待" object要搜索另一个元素。怎么可能?

2 个答案:

答案 0 :(得分:3)

elementToBeClickable期望Run对象以及By对象作为输入,您等到此元素可见且可点击如下: -

WebElement

如果你想等待嵌套元素,请尝试使用期望wait.until(ExpectedConditions.elementToBeClickable(wsearch)); WebElement对象的presenceOfNestedElementLocatedBy等待ByDOM元素出现在{{1}的上下文中1}}如下: -

WebElement

希望它有帮助...:)

答案 1 :(得分:1)

可以使用 WebDriverWait 参考创建自定义函数来协助此过程。

提供的代码示例未经测试

 WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.ignoring(NoSuchElementException.class);

        final String testName = "";
        final WebElement wsearchlist = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
        WebElement wsearch = wait.until(new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver input) {
                By by = new By.ByLinkText(testName);
                WebElement search = null;
                search = wsearchlist.findElement(by);
                return search;
            }}); 

        //Continue on.

如果wait.ignoring调用对NoSuchElementException不起作用,则将try / catch块添加到函数

WebDriverWait wait = new WebDriverWait(driver, 10);
        //wait.ignoring(NoSuchElementException.class);

        final String testName = "";
        final WebElement wsearchlist = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
        WebElement wsearch = wait.until(new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver input) {
                By by = new By.ByLinkText(testName);
                WebElement search = null;
                try {
                    search = wsearchlist.findElement(by);
                } catch (NoSuchElementException nsee) {
                    nsee.printStackTrace();
                    //FIXME:  LOG THIS
                }
                return search;
            }}); 

        //Continue on.