1

I'm currently testing a website with python-selenium and it works pretty well so far. I'm using webdriver.Firefox() because it makes the devolepment process much easier if you can see what the testing program actually does. However, the tests are very slow. At one point, the program has to click on 30 items to add them to a list, which takes roughly 40 seconds because the browser is responding so awfully slowly. So after googling how to make selenium faster I've thought about using a headless browser instead, for example webdriver.PhantomJS().

However, the problem is, that the website requires a login including a captcha at the beginning. Right now I enter the captcha manually in the Firefox-Browser. When switching to a headless browser, I cannot do this anymore.

So my idea was to open the website in Firefox, login and solve the captcha manually. Then I somehow continue the session in headless PhatomJS which allows me to run the code quickly. So basically it is about changing the used driver mid-code.

I know that a driver is completely clean when created. So if I create a new driver after logging in in Firefox, I'd be logged out in the other driver. So I guess I'd have to transfer some session-information between the two drivers.

Could this somehow work? If yes, how can I do it? To be honest I do not know a lot about the actual functionality of webhooks, cookies and storing the"logged-in" information in general. So how would you guys handle this problem?

Looking forward to hearing your answers, Tobias

Note: I already asked a similar question, which got marked as a duplicate of this one. However, the other question discusses how to reconnect to the browser after quitting the script. This is not what I am intending to do. I want to change the used driver mid-script while staying logged in on the website. So I deleted my old question and created this new, more fitting one. I hope it is okay like that.

eto3542
  • 71
  • 5

2 Answers2

0

The real solution to this is to have your development team add a test mode (not available on Production) where the Captcha solution is either provided somewhere in the page code, or the Captcha is bypassed.

Your proposed solution does not sound like it would work, and having a manual step defeats the purpose of automation. Automation that requires manual steps to be taken will be abandoned.

anonygoose
  • 741
  • 3
  • 11
0

The website "recognizes" the user via Cookies - a special HTTP Header which is being sent with each request so the website knows that the user is authenticated, has these or that permissions, etc.

Fortunately Selenium provides functions allowing cookies manipulation so all you need to do is to store cookies from the Firefox using WebDriver.get_cookies() method and once done add them to PhantomJS via WebDriver.add_cookie() method.

firefoxCookies = firefoxDriver.get_cookies()
for cookie in firefoxCookies:
    phantomJSDriver.add_cookie(cookie)
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • You will also need to fetch a page with the new driver instance before adding cookies to it since you are not able to set cookies for domains other than the domain of the currently loaded page. – Corey Goldberg Jun 21 '19 at 16:44
  • I tried it like this: Go to site with normal firefox driver -> Login with normal firefox driver -> Go to site with headless firefox driver -> Copy cookies from normal firefox driver using your commands (works) -> Go to site with headless browser again -> Unluckily I am not logged in in the headless driver though :/ – eto3542 Jul 03 '19 at 18:19