有什么方法我们可以回去尝试阻止捕获

时间:2014-03-05 09:27:20

标签: 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;
    }
  }
}

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

5 个答案:

答案 0 :(得分:4)

你可以在while循环

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

答案 1 :(得分:2)

try/catch阻止置于while循环:

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

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

答案 2 :(得分:1)

使用xpath作为参数创建一个新方法,并使用try-catch块来调用driver.findElement
使用此方法代替直接调用driver.findElement

答案 3 :(得分:0)

您可以引入一些逻辑来重新执行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);

答案 4 :(得分:0)

无法从catch返回try阻止。

您必须重新设计代码 - 将每个findElement()方法包装在单独的try/catch块中,或者使用其他答案中建议的循环。 (我想在你的代码中会有太多的循环 - 第二个将迭代你的所有xpath参数,如:"//td/div/div/div/a"等。)。