java切换浏览器,如何使用Java在Selenium Webdriver中的2个浏览器之间切换

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.

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

This is what i have done:

@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();

解决方案

When you do

WebDriver driver = new ChromeDriver();

driver = new ChromeDriver();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值