@Test
public void TestSetup() throws IOException, BiffException
{
System.setProperty("webdriver.chrome.driver","C:\\Users\\yirsh\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
FileInputStream fi = new FileInputStream("C:\\Users\\yirsh\\Desktop\\UAT WEBSHOP.xls");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet("Regro");
for(int row=1; row <=2 ; row++)
{
driver=new ChromeDriver();
driver.manage().deleteAllCookies();
wait = new WebDriverWait(driver,30);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http:/path/");
driver.manage().window().maximize();
String username = s.getCell(2,row).getContents();
System.out.println("***********************************");
System.out.println("Username: "+username);
driver.findElement(By.id("j_username_leftAside")).sendKeys(username);
String password= s.getCell(3,row).getContents();
System.out.println("Password: "+password);
driver.findElement(By.id("j_password_leftAside")).sendKeys(password);
driver.findElement(By.xpath("/html[@class=' js opacity generatedcontent pointerevents']/body[@class='page-homepage pageType-ContentPage template-pages-layout-RexelHomePageLayout pageLabel-homepage language-de ']/div[@id='page']/div[@id='content']/div[@id='content']/div[@class='content-top-inner']/div[@id='content-inner']/div[@class='mid-wrapper'][1]/div[@class='yCmsContentSlot']/div[@class='login clear']/form[@id='loginForm']/div[@class='left sign-in']/button[@class='Sign-in rx-btn mb0']")).click();
try
{
if((driver.findElement(By.xpath(".//*[@id='globalMessages']/div"))).isDisplayed())
{
System.out.println("Login Failed");
String Error=driver.findElement(By.xpath(".//*[@id='globalMessages']/div")).getText();
System.out.println("The Error mesaage is :"+Error);
System.out.println("***********************************************************************************************************");
}
}
catch (Exception e)
{
System.out.println("Login Sucessfull");
System.out.println("***********************************");
driver.findElement(By.xpath(".//*[@id='content-inner']/div[1]/div/div[2]/div[4]/div/div/ul/li[9]/a")).click();
}
driver.close();
}
}
每次浏览器打开并执行此脚本并输入凭据,如果失败或通过将关闭。但我需要的是打开浏览器后检查第一个凭据是否通过注销然后尝试使用下一个凭证。如果失败输入下一个凭证并继续。因此,我相信打开浏览器驱动程序的时间会减少。请帮助我如何在此代码中进行更改,因为我是初学者请详细说明步骤
答案 0 :(得分:1)
我认为这可能就是你所追求的。您当前的代码告诉它关闭并重新打开一个新的驱动程序实例。这将打开一个驱动程序实例并保持打开以延长for循环的寿命,然后在循环迭代完成后关闭它。
driver = new ChromeDriver();
for(int row=1; row <=2 ; row++)
{
...
}
driver.close();