Issue
I am running a code in Selenium webdriver and whenever an element is not found it goes to catch. But I want java to continue the execution after skipping that element.
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;
}
}
}
so now I want the execution to go back to try block after firing the exception. Is there anyways to do so.
Solution
Put the try/catch
block inside the while
loop:
while (x < 10)
{
try
{
...
}
catch (NoSuchElementException e)
{
...
}
}
BTW, you should also increment x
inside the loop...
Answered By - barak manos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.