2

I have the following scenario:

I have a C# WPF program where I try to access different websites at the same time and collect data from them.

Website A (www.example.com) has a login form and after login displays a number of links such as www.example.com?redir=abc

What I do:

  • start FirefoxDriver 1
  • login
  • infinite loop: collect all the links & refresh page

The problem is, I have another FirefoxDriver instance 2 running in a different Thread in my Project.

In this Instance 2 I access the links that I collected before and want to open them.

The problem is that in order to access them (they are redirecting), I need to login again to website A. When I login, the session on Instance 1 is ended so I need to login there again etc. etc., so effectively the instances are "stealing" each other sessions. (I want to scale this even more to have instances 3,4...so that makes the problem even worse).

Obviously my problem is that instance 1 and instance 2 are not sharing the same session.

Possibilities that I found to solve the issue:

  • using the same FirefoxProfile : tried, not working, still doesn't share session
  • using one instance with 2 windows/tabs and access it from both Threads using the Selenium ThreadGuard class : seems very complicated since I would need to switch between windows all the time
  • using Selenium Grid: seems overkill and I couldn't yet figure out if that's the right thing for me

Therefore I am asking, anyone has an advice how I should design this?

Thank you for your help.

2
  • A bit confused why you are even using threading for this?
    – Arran
    Sep 30, 2014 at 11:12
  • because I need to update&read from Website A constantly as often as possible and at the same time do stuff on the second website B. I assumed threads were the right thing to do and actually have no clue how I could do it otherwise. Any suggestions maybe?
    – skhro87
    Sep 30, 2014 at 15:09

1 Answer 1

9
+50

First I thought there was a way to share a profile between two instances, but that doesn't seem to be the case with WebDriver. The only way I was able to get it working was to copy the cookies from one instance to the other:

var driver1 = new FirefoxDriver();
driver1.Navigate().GoToUrl("http://www.html-kit.com/tools/cookietester/");
driver1.FindElementByXPath("//input[@value=\"Set Test Cookie\"]").Click();

var driver2 = new FirefoxDriver();
driver2.Navigate().GoToUrl("http://www.html-kit.com/tools/cookietester/");

// Copy cookies from one driver to the other
foreach (Cookie c in driver1.Manage().Cookies.AllCookies)
{
    driver2.Manage().Cookies.AddCookie(new Cookie(c.Name, c.Value, c.Domain.TrimStart('.'), c.Path, c.Expiry));
}

driver2.Navigate().Refresh();

Only issue I had to work around was that WebDriver (or Firefox) does not let you set cookies if you are not on the same domain. So a little trickery is required: go to a URL on the same domain and a refresh or navigate to the target URL later. So in your case:

  • Driver1: Navigate to SiteA
  • Driver1: Login
  • Driver2: Navigate to SiteA
  • Copy cookies from Driver1 to Driver2
  • Driver2: Refresh (Should be logged in now)
  • Driver2: Navigate to SiteB

Bit of a scenario. I hope it works. Good luck.

2
  • thanks! it works at least most of the times...i got some strange behavior when I navigate to specific url in same website...working on fixing this. hope I can finally use this solution in my particular use case
    – skhro87
    Oct 9, 2014 at 4:47
  • 1
    thanks for the trick of setting the second driver in the same domain to allow the cookies to be copied.
    – dlcardozo
    May 31, 2020 at 14:51

Your Answer

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

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