我已经为selenium web驱动程序中的登录参数化编写了脚本,用于有效和无效的条目。脚本运行正常,有效输入,但卡住无效输入现在我想处理无效条目的错误,并希望恢复我的脚本下一次输入。那我怎么能这样做呢。请帮帮我。
检查以下代码
public class Sample {
public static void main(String[] args) throws InterruptedException, BiffException, IOException {
//Login Page
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
//Login parameterization using excel sheet (Jxl.jar)
Workbook wBook = Workbook.getWorkbook(new File("F:\\Selenium\\Parameterize\\login.xls"));
int i = 1;
//Get sheet
Sheet sheet = wBook.getSheet(0);
for(i = 1; i<sheet.getRows(); i++){
driver.get("https://www.Google.com");
//user name
driver.findElement(By.xpath("//*[@id='j_username']")).sendKeys(sheet.getCell(0, i).getContents());
//password
driver.findElement(By.xpath("//*[@id='j_password']")).sendKeys(sheet.getCell(1, i).getContents());
//submit
driver.findElement(By.xpath("//*[@id='signInForm']/div/div/div[7]/div[2]/button")).click();
Thread.sleep(5);
//TO move mouse to display Sign Out drop down window
Actions builder = new Actions(driver);
WebElement getElement = driver.findElement(By.xpath("//*[@id='welcomeSpanUserName']"));
builder.moveToElement(getElement).build().perform();
Thread.sleep(5);
//Click on Sign Out button
driver.findElement(By.xpath("//*[@id='wrapper']/div[2]/ul[2]/li[2]/ul/li[2]/span/a")).click();
}
}
}
答案 0 :(得分:1)
尝试在循环中使用try..catch块,如果有异常则继续下一次迭代。
public class Sample {
public static void main(String[] args) throws InterruptedException, BiffException, IOException {
//Login Page
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
//Login parameterization using excel sheet (Jxl.jar)
Workbook wBook = Workbook.getWorkbook(new File("F:\\Selenium\\Parameterize\\login.xls"));
int i = 1;
//Get sheet
Sheet sheet = wBook.getSheet(0);
for(i = 1; i<sheet.getRows(); i++){
try{
driver.get("https://115.112.36.94:7443/");
//user name
driver.findElement(By.xpath("//*[@id='j_username']")).sendKeys(sheet.getCell(0, i).getContents());
//password
driver.findElement(By.xpath("//*[@id='j_password']")).sendKeys(sheet.getCell(1, i).getContents());
//submit
driver.findElement(By.xpath("//*[@id='signInForm']/div/div/div[7]/div[2]/button")).click();
Thread.sleep(5);
//TO move mouse to display Sign Out drop down window
Actions builder = new Actions(driver);
WebElement getElement = driver.findElement(By.xpath("//*[@id='welcomeSpanUserName']"));
builder.moveToElement(getElement).build().perform();
Thread.sleep(5);
//Click on Sign Out button
driver.findElement(By.xpath("//*[@id='wrapper']/div[2]/ul[2]/li[2]/ul/li[2]/span/a")).click();
}catch(Exception e){
//handle exception if you need
}
}
}
}