Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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 尝试捕获,直到成功_Java_Selenium_Try Catch - Fatal编程技术网

Java 尝试捕获,直到成功

Java 尝试捕获,直到成功,java,selenium,try-catch,Java,Selenium,Try Catch,我得到陈旧的元素引用异常。如果我可以单击。 我想单击元素,直到或5次找到该对象 如何在找到单击或元素之前尝试捕捉块循环? 我希望它尝试点击元素5次,然后失败。 我使用下面的代码,但不工作 单击方法: public void click1(WebDriver driver, WebElement element, String name) { int attempts = 0; while(attempts < 5) { try {

我得到陈旧的元素引用异常。如果我可以单击。 我想单击元素,直到或5次找到该对象

如何在找到单击或元素之前尝试捕捉块循环? 我希望它尝试点击元素5次,然后失败。 我使用下面的代码,但不工作

单击方法:

public void click1(WebDriver driver, WebElement element, String name) {    
    int attempts = 0;
    while(attempts < 5) {
        try {
            element.click();
            Add_Log.info("Successfully clicked on  " + name);
            Reporter.log("Successfully clicked on " + name);
            break;
        } catch (Exception e) {
            try {
                JavascriptExecutor executor = (JavascriptExecutor) driver;
                executor.executeScript("arguments[0].click();", element);
                Add_Log.info("Successfully clicked on  " + name);
                Reporter.log("Successfully clicked on " + name);
                break;
            } catch (Exception e2) {
                Add_Log.info("Not able to click " + name);
                Reporter.log("Not able to click " + name);
                TestResultStatus.Testfail = true;
                Assert.fail();
            }
        }
        attempts++;
    }
}
public void click1(WebDriver驱动程序、WebElement元素、字符串名){
int=0;
while(尝试次数<5次){
试一试{
元素。单击();
添加日志信息(“成功单击”+名称);
Reporter.log(“成功点击”+名称);
打破
}捕获(例外e){
试一试{
JavascriptExecutor executor=(JavascriptExecutor)驱动程序;
executor.executeScript(“参数[0]。单击();”,元素);
添加日志信息(“成功单击”+名称);
Reporter.log(“成功点击”+名称);
打破
}捕获(异常e2){
添加日志信息(“无法单击”+名称);
Reporter.log(“无法点击”+名称);
TestResultStatus.Testfail=true;
Assert.fail();
}
}
尝试++;
}
}

这实际上与将循环条件从
尝试<5
更改为
true
并删除
尝试++一样简单行。至少我从你的问题中了解到了这一点。如果这不是你想要的,试着把问题说得更清楚。

我会写得更像下面的内容。您尝试正常单击5次,尝试之间有短暂的停顿。如果抛出异常,将吃掉它并进行另一次尝试。如果这5个都不成功,您可以尝试使用JS单击它。如果失败,则记录失败等

public void click1(WebDriver driver, WebElement element, String name) {    
    int attempts = 0;
    while(attempts < 5) {
        try {
            element.click();
            Add_Log.info("Successfully clicked on " + name);
            Reporter.log("Successfully clicked on " + name);
            return;
        } catch (Exception e) {
        }
        attempts++;
        Thread.Sleep(500); // brief pause between attempts
    }

    try {
        JavascriptExecutor executor = (JavascriptExecutor) driver;
        executor.executeScript("arguments[0].click();", element);
        Add_Log.info("Successfully clicked on " + name);
        Reporter.log("Successfully clicked on " + name);
        return;
    } catch (Exception e2) {
        Add_Log.info("Not able to click " + name);
        Reporter.log("Not able to click " + name);
        TestResultStatus.Testfail = true;
        Assert.fail("Not able to click " + name);
    }
}
public void click1(WebDriver驱动程序、WebElement元素、字符串名){
int=0;
while(尝试次数<5次){
试一试{
元素。单击();
添加日志信息(“成功单击”+名称);
Reporter.log(“成功点击”+名称);
返回;
}捕获(例外e){
}
尝试++;
Thread.Sleep(500);//尝试之间的短暂停顿
}
试一试{
JavascriptExecutor executor=(JavascriptExecutor)驱动程序;
executor.executeScript(“参数[0]。单击();”,元素);
添加日志信息(“成功单击”+名称);
Reporter.log(“成功点击”+名称);
返回;
}捕获(异常e2){
添加日志信息(“无法单击”+名称);
Reporter.log(“无法点击”+名称);
TestResultStatus.Testfail=true;
Assert.fail(“无法单击”+name);
}
}

注意:在尝试单击元素之前,最好先等待该元素可单击。这可能会解决你的大部分问题。您可能仍然存在一些对话框/横幅/微调器与需要处理的元素重叠的问题。。。你可以通过5次尝试点击等方式来解决这个问题。

所以。。。您想在循环中执行某些操作,直到满足条件为止?你试过写循环吗?您的代码中具体有哪些“不起作用”?