2

We use different profiles to test different features of our system. If we open new multiple FF sessions (each with it's own profile), Selenium Webdriver only uses 1 window handle identifier.

(pseudo code below)

WebDriver driver1 = new FirefoxDriver(Profile1);

WebDriver driver2 = new FirefoxDriver(Profile2);

WebDriver driver3 = new FirefoxDriver(Profile3);

String h1 = driver1.getWindowHandle();

String h2 = driver3.getWindowHandle(); 

String h3 = driver3.getWindowHandle(); 

EVERY ONE of the window handles will be identical, even though these were new sessions with unique profiles. (That is, h1 == h2 == h3 )

Am I missing something here? I would have thought that the handles would be unique for each window?

-Dennis

1
  • i am also stuck in this matter...i am using multiple profiles same as you in chromedriver but the window handle is same for all the instances of webdriver ...did you find anything on that? Oct 17, 2022 at 22:24

2 Answers 2

0

It shouldn't happen given, at least I cannot reproduce your issue:

enter image description here

each time the handle represents an unique GUID structure.

Consider the following checklist:

  1. Make sure you use thread-safe collections, functions and patterns
  2. Avoid using static fields
  3. Implement thread-local storage pattern at least for WebDriver class instances

Check out Parallel Tests Execution article for more details and example sample project you can use as the reference.

1
  • Hi Dmitri, thanks for the info. I inherited a lot of code, and some of it is a class that extends FireFoxDriver, with some static methods. I'll investigate the implications of that and your other suggestions. Best regards, Dennis May 17, 2019 at 22:23
0

The issue you are facing is because the window handles returned by the WebDriver are not unique to the browser session or profile. Instead, they are unique to the window itself. When you open a new window or tab using WebDriver, it is treated as a child window of the original window. Hence, both the parent window and the child window have the same window handle.

To work around this issue, you can switch between the different windows using the driver.switchTo().window(handle) method. You can get the list of all open window handles using driver.getWindowHandles() method and then switch between them as per your requirement.

Here's an example of how you can switch between windows:

// Open a new window
WebDriver driver1 = new FirefoxDriver(Profile1);

// Open another new window
WebDriver driver2 = new FirefoxDriver(Profile2);

// Switch to the first window and perform some actions
driver1.switchTo().window(driver1.getWindowHandle());
driver1.get("https://www.example.com");
// perform some actions

// Switch to the second window and perform some actions
driver2.switchTo().window(driver2.getWindowHandle());
driver2.get("https://www.google.com");
// perform some actions

You can find more information about working with multiple windows in Selenium WebDriver in the official documentation: hhttps://www.selenium.dev/documentation/webdriver/interactions/windows/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.