Browser profile in selenium - selenium

I have a doubt on how a profile of a browser affects the output of a Selenium script.
Following are the different scenarios:
If any profile is not specified in the Selenium script, then what profile is opened by Selenium? Is it the default profile the browser has or any new profile assigned by Selenium Webdriver?
Let say there is a profile "ABC" specified in a Selenium script. If the script clicks on a link that opens a new window then the new window has which profile? Is it in the same profile "ABC" or any different profile?
If profiles of a different windows are different then has it any effect on cookies and session?

This differs slightly from browser to browser.
On IE, there are obviously no profiles, so the default one is used.
On FF and Chrome (I don't know about Opera), if there's no profile specified, Selenium opens up with a new, clean profile every time.
Any new window opened by WebDriver is of course opened via the current profile. Once WebDriver is instantiated, it used the assigned profile.
All cookies and sessions behave according to the profile settings.

Related

Python Selenium Safari

I don't understand how this works:
If I open a website by hand in for example Safari or Chrome, I can log me on and if I close the browser I automatically log in after reopen the browser. So I once log in in Instagram, the next time I don't have to do it again if I reopen the side.
But if I open it with selenium, it won't work like this. Is there a way to give options in selenium to fix this? I can't open a side without the side thinks iam a robot (what actually is right) but this sucks...
When you quit the webdriver, the cookies, cache, history, etc are deleted. So if you want to be already logged in when you start the webdriver, you have to use the cookies from a profile on the normal browser in which you are already logged in. To do so you have to:
create a new chrome profile on the normal browser
login to instagram
load the profile in selenium
In this way when you start the webdriver and run driver.get('https://www.instagram.com/') you will be already logged in.
To create a new profile, open your chrome browser, click on the profile icon on top right, then click "Add" and then click "Continue without an account".
Then write a name for your profile and be sure check the box "Create a desktop shortcut". I suggest you to choose a color so that you will immediately see if it works when selenium open the browser window.
Open chrome from the new desktop icon (mine is called "PythonSelenium - Chrome.exe") and login to instagram.
After you logged in, close the browser and open the properties of the new desktop shortcut. Take note of the name of the profile directory, which in my case is "Profile 3".
Now we have to tell selenium to open the chrome driver using this new profile we've just created, which is logged in twitch. To do this we need the path of the folder where the profile is stored. By default the path is something like C:\Users\your_username\AppData\Local\Google\Chrome\User Data, if you are not sure about it check where chrome is installed in your computer.
Then run this code (remember to substitute Profile 3 with the name of your profile from step 3)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\your_username\\AppData\\Local\\Google\\Chrome\\User Data")
options.add_argument("profile-directory=Profile 3") # <-- substitute Profile 3 with your profile name
chromedriver_path = '...'
driver = webdriver.Chrome(options=options, service=Service(chromedriver_path))
driver.get('http://twitch.tv/login') # it should redirect you to the homepage
And this is the result:

Selenium: Point towards default Chrome session

Though I realize it's NOT "good" practice - I have a use case where I need to point (hook up) the Selenium driver to my default Chrome session/profile.
My default profile is here:
~/Library/Caches/Google/Chrome/Default
Here is how I'm seting it up currently: (not working)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Caches/Google/Chrome")
options.add_argument("--profile-directory=Default")
browser = webdriver.Chrome(options=options, executable_path=r"./chromedriver")
browser.get("http://google.com")
I'm using Chrome version 74.0.3729.169 and chromedriver version ChromeDriver 74.0.3729.6 (which is the compatible version).
When Chrome opens I don't see any cookies in Chrome's settings so it's clear it's NOT being pointed to my default session. Also, I see that a Selenium directory has been created (which appears to mean that it has failed to connect to the session at ~/Library/Caches/Google/Chrome/Default.
How do I hook up selenium to my default Chrome session? This is the same session as one sees when normally opening up Chrome.
I've looked at this other question, but the answer there fails to address how to point Selenium towards default session. Also - it's an outdated question - Chrome and Chromedriver have progressed a lot since then. Also, the question there assumes that the poster is able to connect to default session - I am not able to do that which suggests that the Chromedriver/Chrome have changed since then. Also that question is for Windows - I'm on a Mac where things work differently.
Make sure you are pointing to the right folder using "Chrome://version".
I am using the windows but it should be similar in you mac case too.
Refer to this link for more information.
How to create a custom profile:
You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the user-data-dir switch set to some new directory. If the path doesn't exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open chrome://version in the browser to see what profile Chrome is using.
Reference:
http://chromedriver.chromium.org/capabilities
To start with, No, you can't point (hook up) the Selenium driver to any of the existing/previous Web Browsing session. Even if you are able to extract the Session ID, Cookies and other session attributes from the existing/previous Web Browsing session, still you won't be able to pass those attributes as a HOOK to the WebDriver.
You can find a detailed discussion in How can I reconnect to the browser opened by webdriver with selenium?
But of coarse you can connect to the existing Default Chrome profile.
You seem to be already aware that trying to use the Default Chrome Profile for Test Automation will be against all the best practices as the Default Chrome Profile may contain either/all of the following:
browser settings
Extensions
Bookmarks
Apps
Saved Passwords
Browsing History
etc
So the Default Chrome Profile may not be in compliance with you Test Specification and may occasionally raise exception while trying to load. Hence you should always use a customized Chrome Profile.
You can find a detailed discussion in How to open a Chrome Profile through --user-data-dir argument of Selenium
If your usecase still warrants to use the Default Chrome Profile you need to follow the below mentioned details.
Location of Default Chrome Profile
As per the documentation in How to Find Your Chrome Profile Folder on Windows, Mac, and Linux the location for Chrome’s default profile folder differs depending on your platform. The locations are:
Windows 7, 8.1, and 10: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
Mac OS X El Capitan: Users/<username>/Library/Application Support/Google/Chrome/Default
Linux: /home/<username>/.config/google-chrome/default
You need to replace <username> with the name of your user folder. The default profile folder is simply named Default (or default in Linux). However, if you’ve created additional profiles, their folder names are not as obvious. The name you assigned to the profile when you created it displays on a name button on the right side of the title bar on the Chrome window. Unfortunately, the name Chrome uses on the associated profile folder is a generic, numbered name like Profile 3.
If you need to know any of the Chrome Profile's folder name, you simply need to access chrome://version in the address bar and press Enter.
Snapshot:
The Profile Path shows the location of the current profile. For example, the location of my Default profile in my Windows 10 system is C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\Default. You can select the path and copy it and paste it into File Explorer in Windows, the Finder on OS X or into a file manager like Nautilus in Linux to access that folder.
Sample Code (Windows 10)
Finally, to access the Default Chrome Profile you can use the following Python based solution:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\Soma Bhattacharjee\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
You can find a detailed discussion in How to use Chrome Profile in Selenium Webdriver Python 3

How to get Selenium Webdriver to run on Firefox browser as a different user?

I want to open an application website on Firefox browser using Selenium Webdriver as a different user.
Currently, I am using gecko driver to open that application link, it successfully opens up the browser but unable to open the application link.
But I am able to achieve the same manually by going to Mozilla Firefox icon and clicking on
"Run as different user"
option (as shown in below image) which further open a popup for user credentials and by entering Username and Password of different user there I am able to open the application link in browser.
So, is there any way that I can achieve same process using Selenium Webdriver and opens up my application link.
Thanks in Advance!!
You should rather create Firefox profile by running firefox.exe -p to launch profile manager. Then you can instantiate firefox web driver implementation and configure it to use given profile.
Are you sure that you want to use pre-configured profile? I find much easier to manage to run drivers in private/incognito mode and set cookies manually to configure tests.
You might provide firefox_binary as a parameter for the webdriver:
http://selenium-python.readthedocs.io/api.html
firefox_binary – Instance of FirefoxBinary or full path to the Firefox
binary. If undefined, the system default Firefox installation will be used.
...so, you might use a wrapper script to run firefox as the different user (use sudo in Linux).

Selenium saving browser history

I am using the Firefox webdriver and I need it to save the history of browsed websites. I don't need to do anything with the history, I just need it to be there when I open the history page.
If there is a solution for other webdrivers that would be accepted too, though I would prefer a solution for Firefox.
My first thought was making a custom profile, but Selenium doesn't work with custom profiles, it just creates a new temp profile based on the custom profile.
I also went looking for the temp profile, but I couldn't find it. (Windows 7) I had hoped there would be a way to copy the temp data and place it in a different profile. Right before closing each session, I would then add the history to that profile.
My script is suppose to browse the computer like a normal user would. It self-browses the computer and internet using Python and Selenium. Using temp profiles is not 'normal user behaviour'.
I found some people asking the same question and they all accepted a different way to do what they wanted, but none of those do what I need.
create separated firefox profile using firefox profile manager (win+R and call "firefox -p")
and use this profile in your selenium project on start firefox driver
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("profileToolsQA");
WebDriver driver = new FirefoxDriver(myprofile);
profileToolsQA is your created profile in firefox profile manager
More detailed manual you can find here: http://www.toolsqa.com/selenium-webdriver/custom-firefox-profile/

How do I enable popups in a new Firefox profile using selenium webdriver (Java)

The application I am testing requires pop-up to be enabled. When a launch a new Firefox profile I get the browser message
Firefox prevented this site from opening a pop-up window
with a button on the right called Options. I can manually click the Options button and select to Allow popups from ....
Question is how can I use profile.setPreference to set to allow popups from my website and also not show this Firefox message?
You can save your current Firefox prifile with allowed popups and load it from file like in this case: https://stackoverflow.com/a/6830109/1165331
save you current working profile with popups via browser and loat it.