0

I've created a script to log in to a site using IE. What I want to do is when I'm logged in, the script should transfer cookies from IE to Selenium so that I can do the browsing using Selenium.

I can do the login and browsing using IE or Selenium alone but my point here is to learn how to transfer cookies from IE to Selenium.

I've tried with:

Sub Login()
    Const Url$ = "https://www.facebook.com/"
    Dim Htmldoc As HTMLDocument, targetUrl$
    Dim elem As Object, posts As Object, collectedcookie
    Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Visible = True
        .navigate Url
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set Htmldoc = .document
        Application.Wait Now + TimeValue("00:00:02")
        Htmldoc.querySelector("input[id='email']").Value = "your_username"
        Application.Wait Now + TimeValue("00:00:02")
        Htmldoc.querySelector("input[id='pass']").Value = "your_password"
        Application.Wait Now + TimeValue("00:00:02")
        Htmldoc.querySelector("input[value='Log In']").Click
        
        collectedcookie = .document.cookie
        targetUrl = IE.LocationURL
        .Quit
    End With

    With New ChromeDriver
        .get (targetUrl)
        .AddArgument "--cookie=" & collectedcookie
        .get (targetUrl)
    End With
End Sub

When I execute the above script, I can see that IE can log me in. However, the driver is leading me to the login page again.

Question: How can I transfer cookies from IE to Selenium?

PS The site used within the script is just a placeholder. I chose that site only because most of the login based sites do not support IE nowadays.

MITHU
  • 113
  • 3
  • 12
  • 41
  • First, by using the ".document.cookie" command, we could get all of the cookies. If you want to get the user identity cookie, perhaps you have to loop through the value and get the identity. Second, when using the existing Identity to open website in Chrome browser. How do you send the identity information, through URL or the Http request header? If using URL method, you could add the user information at the behind of the URL, if using the HTTP request method, I'm not finding the useful information to do it with VBA, so, in my option, I suggest you could re-login it in Chrome browser. – Zhi Lv Jun 24 '20 at 10:52
  • If I have to re-log in using chrome driver, why would I even think of using IE in the first place. As for your question how do I send identity information to chrome, it is cookie which plays the main role to do the doing. If I manage to transfer cookie (all of them, not selectively) from IE to Selenium, I suppose I can reach the target page @Zhi Lv - MSFT. Thanks. – MITHU Jun 24 '20 at 11:05
  • [This is](https://stackoverflow.com/a/42114843/9189799) where I could notice a partially similar method in different language. – MITHU Jun 24 '20 at 11:22
  • According to your provide link, the user uses the python request library to create a post, then get the Auth information and add the cookie into the webdriver. I also try to use the similar method to add cookies (using `driver.Manage().AddCookie()` method), but it seems that in VBA, if I use the [AddCookie method](https://www.selenium.dev/documentation/en/support_packages/working_with_cookies/), it fails. But, I found that the AddCookie method works well in my C# application, so, you could according the link to create a C# or Java App to test it(without using VBA). – Zhi Lv Jun 25 '20 at 08:32
  • Thanks for the pointer @Zhi Lv - MSFT. It really saddens me that I may not achieve the same using vba. – MITHU Jun 25 '20 at 13:55

0 Answers0