有什么方法可以让我们从 catch 返回到 try 块

Leo*_*arD 1 java selenium selenium-webdriver

我在 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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

xyz*_*xyz 5

你可以在while循环中做到

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