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

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

Related

Can we run selenium in our current account logged- in browser?

Can we run selenium in our current account logged in browser ?
Basically if i try logging into google using selenium , its says browser insecure.
i am trying to make a amazon cart auto checkout as a school project
so if i try in my existing browser , my amazon id is already registered and i dont have to sign in again . but if i use amazon login in selenium its asking for signin and 2fa is being sent to my mail id, how to i skip this step and directly go to the logged in page??
please help
You can't use your non-chrome driver browser (aka regular chrome browser). Selenium only works with chrome-drivers. One way remain signed in is to specify a profile in options so that every-time the driver initiates, it loads your cookies and history.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=Amazon")
driver = webdriver.Chrome(options=chrome_options)
From the code above, chrome_options.add_argument("--user-data-dir=Amazon") will create a profile 'Amazon' if not already there, and save cookies and history there.
The next time you run the driver it will load it from 'Amazon'.
Here is a blog that explains how to install a chrome browser remotely on a specific folder by installing a dedicated browser onto it to use selenium with.
This manages all data including cache, history, accounts, etc
for further info refer the link here
Here is a blog that explains how to install a chrome browser remotely on a specific folder by installing a dedicated browser onto it to use selenium with.
This manages all data including cache, history, accounts, etc
for further info refer the link here
https://learn-automation.com/how-to-execute-selenium-scripts-on-already-opened-browser/

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

Is there a way to test web application using Selenium and either Driver Firefox or Chrome on a server Centos7 without graphical interface?

On my local machine, I arrived to test my applications with Selenium with any problems.
But, when I'm doing the same operations on a server Centos7 (I have no graphical interface), I've many errors such as web element not found.
I'm using Docker containers for selenium (hub and nodes). The installation is OK and I can see my drivers on Http://:4444/grid/console.
Does Selenium require a graphical interface for its work?
Yes. Selenium requires the browser GUI to be present - which is also called viewport.
If you want that Selenium execution happen, without the browser GUI to be present, then you need to use a headless browser, which , as the name suggests is headless, which means there would be no GUI for them.
Examples of these headless browser include PhantomJS- link. Now Chrome also has a headless mode - link, which you can specify using ChromeOptions. Cheers!

Browser profile in 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.

How to record the selenium RC scripts due to security alerts in all browser?

How to record scripts in Selenium RC? As of now, it throws security alerts at me (Trusted certificates), I'm so unable to record it. Please help me, how can we overcome this problem?
You have two options
Start the rc with -trustAllSSLCertificates parameters.Your startup command should look like
java -jar selenium-server-standalone.jar -trustAllSSLCertificates
You should create a firefox profile as mentioned here. Be sure to note down the path where the profile is being created. Then open firefox in that profile (refer the same mozilla support page if you don't how to open FF in a profile) and browse your application. You will have then be prompted to accept certificate. Accept the certificate and close the browser. Now manually open firefox using the newly created profile and access the application one more time. It should not show the certificate error now. All you need to do now is to ask selenium to use this new firefoxprofile. This can be done by using the -firefoxProfileTemplate parameter during selenium server startup
java -jar seleniumserver.jar -firefoxProfileTemplate="Path to firefoxprofile folder"