【问题标题】:How to switch between 2 browsers in selenium webdriver with java如何使用java在selenium webdriver中的2个浏览器之间切换
【发布时间】:2017-05-13 10:15:12
【问题描述】:

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

如何在 2 个浏览器之间切换(不是在 2 个选项卡之间切换)?

这就是我所做的:

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

【问题讨论】:

  • 第二个浏览器怎么打开?再次使用new ChromeDriver()
  • yes.. 再次使用 new ChromeDriver() 打开第二个浏览器..

标签: java selenium-webdriver browser


【解决方案1】:

我猜这就是你要找的,

  1. 保留两个浏览器对象
  2. 定义一个在浏览器上执行一组操作的方法
  3. 首先使用第一个浏览器调用此方法,然后再次使用第二个浏览器调用此方法
  4. 然后在第一个浏览器上执行更多操作

【讨论】:

  • 是的。这就是我想做的。我该怎么做?
  • 嘿,你有没有找到解决你问题的方法,我也需要实现这个?
【解决方案2】:

当你这样做时

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

您重新初始化driver 实例,这意味着您丢失了第一个浏览器。可以拨打getWindowHandles()查看

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

如果你想不同的浏览器使用临时驱动

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

// do some stuff on tempDriver

tempDriver.close();

// continue working with the first driver

【讨论】:

  • 但用户只能在第二个浏览器中登录
  • 你怎么知道用户没有登录到第一个浏览器?你有什么错误吗?
  • 是的...我遇到了一个错误。前面的评论有一个小错误。用户通过第一个浏览器登录。我在该行中遇到错误: WebElement login =tempDriver .findElement(By.xpath(properties.getProperty("VAR_LOGIN")));错误是:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//*[@id='main-section']/div /div/section[2]/div[1]/ul/li[1]"}
  • 谁能帮帮我?
  • 我可以在第二个浏览器中访问 URL 和其他文本。但无法访问其中的对象。请提出解决方案
【解决方案3】:

我已经在 Typescript 中解决了浏览器驱动程序之间的切换,但您应该很容易理解该方法并在 Java 中实现它。

@TDriver.ts

    import {browser, ProtractorBrowser} from 'protractor';

    export class TDriver {
      private static defaultBrowser: ProtractorBrowser = browser;
      private static activeBrowser;

      public async newDriver() {
        const secondDriver = TDriver.activeBrowser.forkNewDriverInstance();
        await this.setActiveBrowser(secondDriver);
        }

      public setActiveBrowser(driver: ProtractorBrowser) {
        TDriver.activeBrowser = driver;
      }

      public getActiveBrowser() {
        return TDriver.activeBrowser;
      }

      public setDefaultBrowserActive() {
        TDriver.activeBrowser = TDriver.defaultBrowser;
      }

    }

@Test.ts

import {by} from 'protractor';
import {TDriver} from '../driver/TDriver';

export class Test {
  private driver = new TDriver().getActiveBrowser();

   this.driver.findElement(by.xpath("xPathSelector")).click(); //this click in first native driver instance

   await new TDriver().newDriver(); //create new driver and switch to it!

   this.driver.findElement(by.xpath("xPathSelector")).click(); //this click in the second driver instance

   await new TDriver().setDefaultBrowserActive(); //now we switch and use again native driver

   this.driver.findElement(by.xpath("xPathSelector")).click(); //this click in first native driver instance
}

这些方法使您能够在本机浏览器中执行测试,然后创建新实例并在第二个浏览器驱动程序中继续测试。在我的项目中工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 2015-02-20
    • 2013-10-07
    • 2012-06-08
    • 2020-11-28
    • 2018-08-19
    相关资源
    最近更新 更多