如何使用Java在Selenium Webdriver中的2个浏览器之间切换 [英] How to switch between 2 browsers in selenium webdriver with java

查看:407
本文介绍了如何使用Java在Selenium Webdriver中的2个浏览器之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java开发Selenium Webdriver.我想打开浏览器,在其中执行一些操作.然后打开另一个浏览器并在其中执行相同的操作,然后返回第一个浏览器并执行一些操作.

I'm working on selenium webdriver with java. I want to open a browser perform some actions in it. Then open another browser and do the same actions in it, then go back to first browser and perform some actions.

我如何在2个浏览器之间切换(而不是2个标签之间的切换)?

How can i switch between 2 browsers (not the switching between 2 tabs)?

这就是我所做的:

@BeforeTest
    public void beforeTest() throws BiffException, IOException,InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\MyProjects\\SeleniumTrials\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get(properties.getProperty("VAR_BASEURL"));
        driver.manage().window().maximize();
      WebDriver  tempDriver = new ChromeDriver();
        tempDriver.get(properties.getProperty("VAR_BASEURL"));
        tempDriver.manage().window().maximize();
}
@Test
    public void playTournament() throws InterruptedException, BiffException,IOException {
    int rowNumber = 1;
    int newRowNumber=2;
    WebElement login =driver.findElement(By.xpath(properties.getProperty("VAR_LOGIN"))); 
    login.click();
    Thread.sleep(1000);
    WebElement username = driver.findElement(By.xpath(properties.getProperty("VAR_USERNAME")));
    username.clear();
    username.sendKeys(getCellContent(0, rowNumber));
    Thread.sleep(1000);
    WebElement password = driver.findElement(By.xpath(properties.getProperty("VAR_PASSWORD")));
    password.clear();
    password.sendKeys(getCellContent(1, rowNumber));
    Thread.sleep(1000);
    WebElement continueButton = driver.findElement(By.xpath(properties.getProperty("VAR_CONTINUE")));
    continueButton.click();
    Thread.sleep(1000);

   WebElement login =tempDriver .findElement(By.xpath(properties.getProperty("VAR_LOGIN"))); 
   login.click();
   Thread.sleep(1000);
   WebElement username = tempDriver .findElement(By.xpath(properties.getProperty("VAR_USERNAME")));
   username.clear();
   username.sendKeys(getCellContent(0, rowNumber));
   Thread.sleep(1000);
   WebElement password = tempDriver .findElement(By.xpath(properties.getProperty("VAR_PASSWORD")));
   password.clear();
   password.sendKeys(getCellContent(1, rowNumber));
   Thread.sleep(1000);
   WebElement continueButton = tempDriver .findElement(By.xpath(properties.getProperty("VAR_CONTINUE")));
   continueButton.click();

推荐答案

执行时

WebDriver driver = new ChromeDriver();
driver = new ChromeDriver();

您重新初始化driver实例,女巫意味着您松开了第一个浏览器.您可以通过调用getWindowHandles()

You reinitialize the driver instance, witch means you loose the first browser. You can see it by calling getWindowHandles()

driver.getWindowHandles(); // will be 1, the last open browser

如果您想在其他浏览器中使用临时驱动程序

If you want to different browsers use temporary driver

WebDriver driver = new ChromeDriver();
WebDriver tempDriver = new ChromeDriver();

// do some stuff on tempDriver

tempDriver.close();

// continue working with the first driver

这篇关于如何使用Java在Selenium Webdriver中的2个浏览器之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆