【问题标题】:is there any way by which we can go back to try block from catch有什么方法可以让我们回到 try block from catch
【发布时间】:2014-03-05 17:38:46
【问题描述】:

我在 Selenium webdriver 中运行代码,每当找不到元素时,它就会捕获。但我希望java在跳过该元素后继续执行。

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import net.sourceforge.htmlunit.corejs.javascript.ast.LabeledStatement;

import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label;
import com.sun.org.apache.bcel.internal.generic.GOTO;

public class try_zencart {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://ipadress/"; //I am providing an ip adress
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testTryZencart() throws Exception {
    driver.get(baseUrl + "/zencart/index.php?main_page=index&cPath=1_16");
    try{
    int x = 3;
    int y = 0;
    driver.navigate().refresh();
    while(x<10){
    y=y+1;
    System.out.println("We have now started the " + y + "  iteration, which means that whenever we stop the execution the {$y} iteration was going on.");
    driver.findElement(By.xpath("//td/div/div/div/a")).click();
    driver.findElement(By.xpath("//td/div/div[2]/div/a")).click();
    driver.findElement(By.xpath("//div[2]/div/a[2]")).click();
    driver.findElement(By.xpath("//div[2]/div/a[3]")).click();
    driver.findElement(By.xpath("//div[12]/div/a[4]")).click();
    driver.findElement(By.xpath("//a[5]")).click();
    driver.findElement(By.xpath("//a[6]")).click();
    driver.findElement(By.xpath("//a[7]")).click();
    driver.findElement(By.xpath("//a[8]")).click();
    driver.findElement(By.xpath("//a[9]")).click();
    driver.navigate().refresh();
 }
    }
    catch(NoSuchElementException e)
    {
        System.out.println("\nArjun no good!!");
        driver.navigate().refresh();
    }
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

所以现在我希望执行在触发异常后返回尝试块。有没有办法这样做。

【问题讨论】:

  • 您为什么不在while 循环中直接使用try/catch?
  • 非常感谢大家,您的回复很有价值。是的,它奏效了。

标签: java selenium selenium-webdriver


【解决方案1】:

你可以在while循环中做到这一点

boolean isSuccessful = false;
while(!isSuccessful){
   try{
     // do whatever you want
     isSuccessful = true;
   }catch(Exception e){
     // Exception so lets do it again
   }
}

【讨论】:

  • 非常感谢 Karna,您的回复很有价值。
  • @LeonarD:很高兴我能帮上忙!
【解决方案2】:

将try/catch 块放入 while 循环:

while (x < 10)
{
    try
    {
        ...
    }
    catch (NoSuchElementException e)
    {
        ...
    }
}

顺便说一句,您还应该在循环内增加 x...

【讨论】:

  • 非常感谢 barak,您的回复很有价值,是的,这行得通。
【解决方案3】:

使用 xpath 作为参数并使用 try-catch 块创建一个新方法,您可以在其中调用 driver.findElement。
使用这种方法而不是直接调用driver.findElement。

【讨论】:

    【解决方案4】:

    您可以引入一些逻辑来重新执行您的 try-catch,例如一种方法 - do-while 循环,其中检查循环条件是否已执行!

    boolean reLoop = false;
    do{
       try{
           // your code
       }catch(Exception ex){
          // handle your exception
          reLoop = true;
       }
    }while(tryExecuted);
    

    要考虑的重要一点是infinite-loop,如果您的尝试每次都失败,在这种特定情况下,请考虑应该执行的try 块的最大数量。

    boolean reLoop = false;
    int loopMaxCount = 3; // assumed maximum number of re looping 
    int loopCont = 0; 
    do{
       loopCont++;
       try{
           // your code
       }catch(Exception ex){
          // handle your exception
          if(loopCont < loopMaxCount) // It blocks infinite looping
               reLoop = true;
       }
    }while(tryExecuted);
    

    【讨论】:

    • 非常感谢 Subhrajyoti,您的回复很有价值。
    【解决方案5】:

    没有办法从catch 回到try 块。

    您将不得不重新设计您的代码 - 要么将每个 findElement() 方法包装在单独的 try/catch 块中,要么按照其他答案中的建议使用循环。 (我猜你的代码中会有太多循环——第二个循环会遍历你所有的 xpath 参数,比如:"//td/div/div/div/a" 等)。

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多