Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java While循环和预期条件是否可以一起使用?_Java_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Java While循环和预期条件是否可以一起使用?

Java While循环和预期条件是否可以一起使用?,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,While循环和预期条件是否可以一起使用 Iam使用下面列出的方法获取奇数无法定位元素/超时异常 2。是否可以添加while循环,以便该方法可以多次检查和执行? public void waitAndClickElement(WebElement element) throws InterruptedException { try { this.wait.until(ExpectedConditions.elementToBeClickable(element))

While循环和预期条件是否可以一起使用

  • Iam使用下面列出的方法获取奇数无法定位元素/超时异常 2。是否可以添加while循环,以便该方法可以多次检查和执行?

        public void waitAndClickElement(WebElement element) throws InterruptedException {
        try {
            this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
            System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
        }catch (Exception e) {
            System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
            Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
        }
    }
    
    public void waitAndClickElement(WebElement元素)抛出中断异常{
    试一试{
    this.wait.until(ExpectedConditions.elementToBeClickable(element))。单击();
    System.out.println(“成功单击Web元素:+”);
    }捕获(例外e){
    System.out.println(“无法等待并单击WebElement,异常:+e.getMessage());
    Assert.assertFalse(true,“无法等待并使用定位器“+”)单击WebElement);
    }
    }
    
    是的,这是可能的。大概是这样的:

        boolean clicked = false;
        int attempts = 0;
        while(!clicked && attempts < 5) {
        try {
                this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
                System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
                clicked = true;
         } catch (Exception e) {
                System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
                Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
            }
             attempts++;
        }
    
    boolean clicked=false;
    int=0;
    当(!单击并尝试<5次时){
    试一试{
    this.wait.until(ExpectedConditions.elementToBeClickable(element))。单击();
    System.out.println(“成功单击Web元素:+”);
    单击=真;
    }捕获(例外e){
    System.out.println(“无法等待并单击WebElement,异常:+e.getMessage());
    Assert.assertFalse(true,“无法等待并使用定位器“+”)单击WebElement);
    }
    尝试++;
    }
    

    在while循环中也添加一个最大尝试次数可能是个好主意,这样您就不会陷入无限循环。

    我是否可以再添加一个while,查看内部的最大尝试次数?谢谢您的帮助。不,您可以将其合并到现有的帮助中。我来编辑一下