How to use a certain Firefox profile in Python Selenium binding? - python

I'm looking for a solution similar to presented in this question, except it has to deal with python.

the answer that was accepted is language agnostic. You will need to setup a firefox profile then when starting up selenium rc tell it to use that profile.
You just need to run your tests to use *firefox against the rc that has your profile and when the browser loads it will be the one you are after

Related

Python-3.x-Selenium: Changing the used driver while staying logged in on a website

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.
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.
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)

Firefox profiles with Selenium and Marionette

I didn't find an answer to this. To automate some tests I have used Firefox and Selenium. I thought to give a try to Selenium 3.0 and its Marionette interface. Everything works with an old Firefox binary and the old webdriver way, so my code as such works.
For my tests I have created a Firefox profile, which I then pass to Selenium. Previously I did it like this:
p = webdriver.FirefoxProfile("profilename")
driver = webdriver.Firefox(firefox_profile=p, firefox_binary="/path/to/ff")
This works. My browser starts and whatever modifications it makes to the profile (cookies), seem to be there when I restart the browser with the same profile.
With Marionette/geckodriver the instructions tell me to use capabilities["profile"] for this, and they state this should be "Base64-encoded zip of a profile directory to use as the profile for the Firefox instance".
Ok. First, how do I create a base64-encoded zip in Python? Or do I just use shell for this?
Second, how does this work in practice? If I zip and base64 encode my profile directory, how do I then get the modified version back after my tests complete? If I created this file in shell and kept passing the same file for consecutive attempts, every modification, for example a login cookie, would be lost, and I would need to start from the scratch again, which in this case is not desirable.
I can keep using the old system at least for now, this is just to satisfy my curiosity.
Hannu

Talk to Chrome extensions background conscole via webdriver

I am new to web programming.
I am trying to add parameters to one of my Chrome extension.
I know I can enter, for example, "window.localStorage.setItem()" in the extension console. However, I cannot find a way to navigate my webdriver to that extension background page. I have seen, in the past, people would use chrome//extensions:extension_id as url to get to that page, but now this method seems not to work.
Is there any way that I can go to that page directly without telling my webdriver to click programmer mode and then click the extension?
Thanks in advance. This has been bothered me for hours.
Probably one thing to correct here is that chrome://extensions/ opens up the Extensions on Chrome browser. So this shall work fine :
driver.get("chrome://extensions/")
Just a note now the chrome://extensions/extension_id does not take you to the extension page anymore.
In case you are interested in the Details or Options of an extension, you shall try and access it via the links only since the uri is not consistent for different extensions as well. e.g
Extension 1
chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/options/index.html
Extension 2 ::
chrome-extension://fngmhnnpilhplaeedifhccceomclgfbg/options_pages/support.html
The general format has extension_id but not certainly defines the entire uri to get you to the page.
I would suggest if you want to work around with the Extensions, there is this Management API which seems useful to dive in.

How to create a new (persistent) Firefox profile with Selenium in Python?

Trynig to add a new, persistent, Firefox profile with Selenium. AFAIK, when executing FirefoxProfile(), a new profile is generated using a temporary file. Ideally, this profile should be able to remain available to subsequent processes - even after the creator is closed.
Problem:
Create a new Firefox profile from within Python code. This should return a FirefoxProfile object that is usable with the Firefox webdriver Selenium uses.
The profile created should persist after the process ends - i.e. it should be a full-fledged profile, not just a temporary profile.
Some pointers:
The profiles.ini file seems to be key. I have read some code that uses the Java class ProfilesIni to modify profile information. If this class is available for Python code, it should probably take care of most of this.
If the only way to do this is to manually modify the profiles.ini file, that's acceptable. A better, more standardized solution (one that uses a library or Selenium code) would be preferable, however.
Thanks very much!
If this helps anyone, what needs to be done is run:
firefox[.exe] -CreateProfile <profile_name>
The .exe in brackets is intended to provide for it being run under Windows.
Yes, this does not use the Selenium library in Python, but it does provide the desired result.

Selenium with Python, how do I get the page output after running a script?

I'm not sure how to find this information, I have found a few tutorials so far about using Python with selenium but none have so much as touched on this.. I am able to run some basic test scripts through python that automate selenium but it just shows the browser window for a few seconds and then closes it.. I need to get the browser output into a string / variable (ideally) or at least save it to a file so that python can do other things on it (parse it, etc).. I would appreciate if anyone can point me towards resources on how to do this. Thanks
using Selenium Webdriver and Python, you would simply access the .page_source property to get the source of the current page.
for example, using Firefox() driver:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.example.com/')
print(driver.page_source)
driver.quit()
There's a Selenium.getHtmlSource() method in Java, most likely it is also available in Python. It returns the source of the current page as string, so you can do whatever you want with it
Ok, so here is how I ended up doing this, for anyone who needs this in the future..
You have to use firefox for this to work.
1) create a new firefox profile (not necessary but ideal so as to separate this from normal firefox usage), there is plenty of info on how to do this on google, it depends on your OS how you do this
2) get the firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/2704/ (this automatically saves all pages for a given domain name), you need to configure this to save whichever domains you intend on auto-saving.
3) then just start the selenium server to use the profile you created (below is an example for linux)
cd /root/Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3
java -jar selenium-server.jar -firefoxProfileTemplate /path_to_your_firefox_profile/
Thats it, it will now save all the pages for a given domain name whenever selenium visits them, selenium does create a bunch of garbage pages too so you could just delete these via a simple regex parsing and its up to you, from there how to manipulate the saved pages

Categories