简体   繁体   English

Selenium:如何在 for 循环中获取 webelement 的变化值

[英]Selenium: How to get the changing value of a webelement in a for loop

I am trying to get the value of weblement which changes with time.我试图获得随时间变化的 weblement 价值。 values are - Pending, Approved, Received .值为 - Pending, Approved, Received

I have to stop the loop when the value is changed to Received .当值更改为Received时,我必须停止循环。 I tried below code but it runs successfully for one loop and then stop in the second loop with error that webelement not found with the given xpath .我尝试了下面的代码,但它在一个循环中成功运行,然后在第二个循环中停止,错误是webelement not found with the given xpath

for(int i=0;i<=50;i++)
{
    String exactvalue=driver.findElement(By.xpath("//tr[2]/td")).getText();
        if (exactvalue.equals("Received"))
        {
            break;
        }
        else 
        {
        driver.navigate().refresh();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException("executing sleep interrupted");
        }

        }
    }

Replace Below line from your code.从您的代码中替换下面的行。

Your code :你的代码:

String exactvalue=driver.findElement(By.xpath("//tr[2]/td")).getText();

Replace with :用。。。来代替 :

String exactvalue=driver.findElement(By.xpath("//tr["+i+"]/td")).getText();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM