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
Related
I'm wondering if there is a way to find the parameters that are passed to the selenium web driver if you don't have the source code of the app itself, but the browser is visible and you can interact with it.
By arguments, I mean all the settings that are different from the default ones that the web driver is started with default. For an example user agent, --commands, etc ...
Some options for Chrome can be viewed by entering "chrome://version/" in the browser's location bar and hitting enter.
I am trying to use selenium with .NET (C#) to pull Flash cookies from a particular URL (in this case, a game on kongregate). I know where the flash cookies are stored physically for the browser I use personally. I attempted to access the flash cookies using the physical location in google Chrome's directory (Google\Chrome\User Data\Default\Pepper Data\Shockwave Flash\WritableRoot#SharedObjects), however this has no effect on the flash cookies a gather using selenium. How can I get access to the Shared Object files that are stored through my selenium instance?
Try loading a custom Chrome profile and see if that helps. Here's a quick example.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
See here for more details.
I've got an ASP.NET site which allows users to view/open files from a shared network folder via links which call response.redirect passing in the full location of the relevant file (using a mapped drive value on appserver, not specific server name).
This works fine in IE7 but now w're doing a Win7 upgrade it isn't working in IE9 (or Chrome). For IE9 it displays basic "Internet Explorer cannot display the webpage" message.
I've tried changing it to pass in the path using server instead of mapped drive but that's worse, is tries to start at root of application directory and then acts as if server is a folder within it (and displays "The page cannot be found").
The site probably shoudln't have ever been designed in this way but I need a dirty workaround, no time for complete redesign. Any ideas? If the cause is how newer browsers handle the redirect response is it possible to alter an advanced setting to mimic previous version? I've tried adding site as trusted and also compatibility view.
Sample code:
Response.Redirect("R:\SharedFolder\indexedEmail.msg"); //Where R is mapped to netshare
Try using using the file:// URL prefix. The newer browsers may not be assuming that the URL is pointing to the local file system.
I am using Selenium (2.24) to generate unit tests (for the Visual Studio unit test framework). While using the C# WebDriver for FireFox, it appears that the browser which is fired up by the driver is not finding my website cookies via javascript (I have a javascript file included in the site that looks for cookies and lets me know if they are found). Also, it is not using the browsers image cache, and is always requesting new images from the server. This behavior does not happen when I run my site from the "normal" (not launched by Selenium) FireFox.
The strange thing is that calling the below code in my unit test DOES return my cookie (it just can't be found by my JavaScript)
driver.Manage().Cookies.GetCookieNamed("MyCookie");
How can I configure the driver to respect my cookies and use the browsers image cache? This functionality is key to testing my website.
By default the FirefoxDriver will create a new anonymous Profile each time it starts Firefox. If you want it to use an exiting profile you need to tell it to.
In Java you do it like so:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("MyProfile");
WebDriver driver = new FirefoxDriver(profile);
I'm assuming there's something similar in C#
For cookies: if cookie is marked as "HTTP Only" JavaScript on a page will not be able to see it. As result any code that uses execution of JavaScript on the page will not see this particular cookie.
You can confirm it by using some HTTP debugger (i.e. Fiddler) to see if cookie is set with HttpOnly property. You also can check if running script on a page via dev tools or typing javascript:alert(...) in address bar can see the cookie (document.cookie)
I know there is built-in Internet explorer, but what I'm looking for is to open Firefox/Mozilla window (run the application) with specified URL. Anyone can tell me how to do that in C# (.nET) ?
You can do this:
System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");
This will launch the system defined default browser:
string url = "http://stackoverflow.com/";
System.Diagnostics.Process.Start(url);
Remember that Process.Start(url) might throw exceptions if the browser is not configured correctly.
See ProcessInfo.UseShellExecute
Use the Process class (System.Diagnostics) using the URL as the process name. This will use the system default browser to open the URL. If you specify a browser, you run the risk that the browser doesn't exist.
In Visual Studio click the File -> Browse With... on the menus and then select the browser that you want to use. You can also change the browser there. If the Browse With... menu option doesn't appear then you need to select a project from your solution that that can be launched in a browser.
If you explicitly do not want to use the User's default browser, you can run the browser with the URL as the first argument.
C:\Program Files\Mozilla Firefox>firefox.exe http://google.com
launches Firefox with google for me. But as people have said, you run the risk of it not being installed, or being installed to a different place, etc.