我们可以在ExplicitWait预期条件中传递条件参数吗? [英] Can we pass a conditional parameter in ExplicitWait expected condition?

查看:80
本文介绍了我们可以在ExplicitWait预期条件中传递条件参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是网站.我想做的是,单击按钮,然后显示显示系统IP地址的文本.

This is the website. What I'm trying to do is, click the button and get the text which show the IPAddress of your system.

我可以找到<span>元素,但是一旦单击获取IP地址"按钮,它将显示文本.

I'm able to locate the element which is <span>but it shows the text once the get IP Address button clicked.

所以我担心的是,我们可以在ExplicitWait中使用条件吗?例如

So my concern is can we use conditions in ExplicitWait ? for example

 driver.findElement(By.id("lblIPAddress")).getText().length()>0

这是我要在ExplicitWait中使用的条件参数.

This is conditional parameter which i want to use in ExplicitWait.

我在显式等待中使用textToBePresentInElement方法,但是在这里,我必须传递硬编码的字符串(IP Address: 123.201.110.30).

I'm using textToBePresentInElement method in Explicit wait but here I have to pass that string (IP Address: 123.201.110.30) hard coded.

WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.id("lblIPAddress")), "IP Address: 123.201.110.30"));

即使我能够通过加载程序的invisibilityOf()方法获取文本,但是一旦隐藏它,它的跨度就会显示IPAddress文本.

Even though I am able to get the text by invisibilityOf() method of the loader , once it get hidden then it span shows the IPAddress text.

wait.until(ExpectedConditions.invisibilityOf(driver.findElement(By.xpath("//div[@class='modal12']"))));
System.out.println("IP address After wait = "+ driver.findElement(By.id("lblIPAddress")).getText());

但是我的查询是在显式等待中使用条件,它等待给定时间来检查条件(检查天气文本是否在元素中可用).

But my query is to use condition in Explicit wait where it wait for the given time to check the condition (check weather text is available in the element or not).

这是代码:

public static void main(String args[])
{

    System.setProperty("webdriver.chrome.driver", "D:\\Application\\chromedriver.exe");
    driver = new ChromeDriver(); 
    driver.manage().window().maximize();
    driver.get("https://www.aspsnippets.com/demos/1331/");
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    driver.findElement(By.id("btnGet")).click();
    System.out.println(driver.findElement(By.id("lblIPAddress")).getText().length());
    System.out.println("IP Address = "+ driver.findElement(By.id("lblIPAddress")).getText());       
    WebDriverWait wait = new WebDriverWait(driver, 120);
    wait.until(ExpectedConditions.textToBePresentInElement(driver.findElement(By.id("lblIPAddress")), "IP Address: 123.201.110.30"));
    System.out.println("IP address After wait = "+ driver.findElement(By.id("lblIPAddress")).getText());

}

推荐答案

我认为您不能在显式等待"(预期条件)中使用条件,但是,您可以在FluentWait中使用任何自定义条件.

I don't think you can use conditions in Explicit Wait (Expected Conditions) however, you can use any custom condition in FluentWait.

如果您使用的是Java 8,则可以编写一个带条件的函数并执行它.

If you are using Java 8, you could write a function which takes condition and executes it.

对于您的情况,您可以编写如下内容.

for your case, you could write something like this.

@Test
public void testFluentWait(){

    driver = new ChromeDriver();
    driver.get("https://www.facebook.com/");
    WebElement element = driver.findElement(By.name("websubmit"));

    waitUntilElementHasText(element);

    //If you are using Java 8
    waitUntilCondition(() -> element.getAttribute("type").contains("submit"));

}

//Only if you are using Java 8
public boolean waitUntilCondition(Callable<Boolean> condition){

    Wait wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);


    wait.until(new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            try {
                return condition.call();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    });

    return false;
}

public boolean waitUntilElementHasText(final WebElement element){

    Wait wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);


    wait.until(new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            return element.getText().length() > 0;
        }
    });

    return element.getText().length() > 0;
}

这篇关于我们可以在ExplicitWait预期条件中传递条件参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆