Does the chromedriver used with Python Selenium store cookies - python

So my question is very simple. I would like know each time I run a selenium application using chromedriver, does it start with a blank slate? Or does it store cookies from last the last applicaion?

Selenium creates a temporary profile for each session, since profiles hold all the cookies, history, cache and settings for the browser the answer is no, it doesnt store cookies or any information from previous sessions.
That is if you don't explicitly override it with a custom profile dir when instantiating your driver

Related

Setting Chrome profile in Selenium doesn't bypass Microsoft MFA

I'm trying to incorporate scraping data from the AWS management console into an automation script, and for some reason my company's AWS is behind a Microsoft multi-factor authentication system. This isn't an issue when going to the console link manually, as the browser remembers that in the past I've already gone through the MFA process and I am directed right to the console. When navigating to the same link in Selenium I am instead brought to the MFA page asking my permission to text/call my phone.
I learned that this could be solved by setting Chrome Profile in the Webdriver options.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/myuser/Library/Application Support/Google/Chrome/")
Even after setting this I am still redirected to the MFA page every time. Am I going about this the wrong way? Is there an easy way to check if the Chrome Profile is actually working as expected?
You need to use an actual user data dir not just the folder where the general chrome data is stored.
For the default chrome profile the name would be:
"/Users/myuser/Library/Application Support/Google/Chrome/User Data/Default"
//Add argument and provide path to userdata
Remember to change username to your username.
Option.AddArgument("user-data-dir=c:\Users\~username~\AppData\Local\Google\Chrome\Userdata");
//Add whatever profile number that corresponds to session you want to open
Option.AddArgument("--profile-directory-Profile-10");
*Find profile by using chrome://version flag in session you want to use. The flag will tell you profile number.

How to change firefox profile preference after defining webdriver using Selenium and Python

How to change profile preference after defining driver?
profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_profile=profile)
After some code need to set useragent
profile.set_preference("general.useragent.override", ua)
How to set it without defining new driver?
As per the current implementation of Selenium once you configure the GeckoDriver with specific Capabilities and initialize the firefox session to open a Browsing Context, you cannot change the capabilities runtime. Even if you are able to retrieve the runtime capabilities still you won't be able to change them back.
So, in-order to change the Firefox User Preference you have to initiate a new WebDriver session.
Note:However, you can change the user-agent for Firefox on each run and you can find a relevant discussion in How to change user agent for Firefox webdriver in Python?
Reference
Here is #JimEvans clear and concise comment (as of Oct 24 '13 at 13:02) related to proxy settings capability:
When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.
Outro
You can find a couple of relevant detailed discussions in:
Python and selenium: how to change Firefox's profile multiple times
I believe it’s not possible but I found some workarounds explained in this article, not sure if those approaches are reliable though (or work at all): https://tarunlalwani.com/post/change-profile-settings-at-runtime-firefox-selenium/

Python selenium with chrome webdriver - change user agent

I am running some code with selenium using python, and I figured out that I need to dynamically change the UserAgent after I already created the webdriver. Any advice if it is possible and how this could be done? Just to highlight - I want to change it on the fly, after almost each GET or POST request I send
I would go with creating a new driver and copy all the necessary attributes from the old driver except the user agent.

Retrieving all cookies from IE using python

I've been writing automated tests with Selenium Webdriver 2.45 in python. To get through some of the things I need to test I must retrieve the various JSESSION cookies that are generate from the site. When I use webdrivers get_cookies() function with Firefox or Chrome all of the needed cookies return to me. When I do the same thing with IE11 I do not see the cookies that I need. Anyone know how I can retrieve session cookies from IE?
What you describe sounds like an issue I ran into a few months ago. My tests ran fine with Chrome and Firefox but not in IE, and the problem was cookies. Upon investigation what I found is that my web site had set its session cookies to be HTTP-only. When a cookie has this flag turned on, the browser will send the cookie over the HTTP(S) protocol and allow it to be set by the server in responses but it will make the cookie inaccessible to JavaScript. (Which is consistent with your comment that you cannot see the cookies you want in document.cookie.) It so happens that when you use Selenium with Chrome or Firefox, Selenium is able to ignore this flag and obtain the cookies from the browser anyway. However, it cannot do the same with IE.
I worked around this issue by turning off the HTTP-only flag when running my site in testing mode. I use Django for my server so I had to create a special test_settings.py file with SESSION_COOKIE_HTTPONLY = False in it.
There is an open issue with IE and Safari. Those driver will not return correct cookies information. At least not the domain. See this

selenium webdriver python: opening chrome browser to run test case does not load the saved settings of the browser

I am trying to run a selenium test case in Chrome browser. Before this i have set the default download location for files of chrome browser to say f:/xyz
When the selenium script is run, which clicks on a link to download a file - it downloads in the chrome's default location (c:/documents and settings/downloads) and not in the f:/xyz which i have set earlier
How to correct this ?
More than likely, the problem here is that the download location is associated with a specific user profile, and the Chrome driver follows the same pattern as the Firefox driver in that by default, it uses a copy of a completely clean user profile every time it is run, so the download location you set for your user is never picked up by Selenium. In Firefox, the solution is to create a custom Firefox profile, then tell Selenium to run with that. I'd bet there is an analogous function in the Chrome driver.
According to docs:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir=/path/to/profile/directory"));
WebDriver driver = new ChromeDriver(capabilities);
Yes that is Java, but it should be fairly easy to translate to Python. Also, note the docs say that there is a known bug about being able to set a custom profile.
Edit:
I think I found a mildly hacky solution that should work for you.
Go to the master folder that contains user/home folders on the OS you are running under
Under the SYSTEM user folder, find the Chrome user data directory
Open the Preferences file (it's raw text, so any text editor will work)
Under the "download" node, create or modify the "default_directory" node to be whatever download location you want
Note that these steps assume that Selenium has actually run Chrome at least once under the SYSTEM user. If not, you can manually create the directories needed by running Chrome under the SYSTEM user yourself, from the terminal for instance.
It can't be done at the time. From the official ChromeDriver Wiki at http://code.google.com/p/selenium/wiki/ChromeDriver:
Known Issues
There are a handful of known issues with ChromeDriver, listed below:
Can only retrieve the name and value of set cookies (no domain, path, etc.)
Typing does not work with rich-text enabled documents.
Cannot specify a custom profile
HTML 5 API not implemented

Categories

Resources