How Might I Simulate a Private Browsing Experience in Watir? (Selenium)

How might I simulate a private browsing experience in Watir? (Selenium)

For Firefox (I am not sure about the other browsers), you can setup the profile to have private browsing enabled:

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.privatebrowsing.dont_prompt_on_enter'] = true
profile['browser.privatebrowsing.autostart'] = true
b = Watir::Browser.new :firefox, :profile => profile

This was the solution from https://github.com/watir/watir-webdriver/issues/95.
It seems to work (at least the main menu says it is private browsing).

Python/Selenium incognito/private mode

First of all, since selenium by default starts up a browser with a clean, brand-new profile, you are actually already browsing privately. Referring to:

  • Python - Start firefox with Selenium in private mode
  • How might I simulate a private browsing experience in Watir? (Selenium)

But you can strictly enforce/turn on incognito/private mode anyway.

For chrome pass --incognito command-line argument:

--incognito Causes the browser to launch directly in incognito mode.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')

FYI, here is what it would open up:

happy holidays!

For firefox, set browser.privatebrowsing.autostart to True:

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.privatebrowsing.autostart", True)

driver = webdriver.Firefox(firefox_profile=firefox_profile)

FYI, this corresponds to the following checkbox in settings:

Sample Image

watir-webdriver cookie jar saving and loading

browser = Watir::Browser.new :firefox
browser.goto 'http://google.com'
# save cookies
saved_cookies = browser.cookies.to_a
# clear and get new cookies
browser.cookies.clear
browser.goto 'http://google.com'
# set new cookies
browser.cookies.clear
saved_cookies.each do |saved_cookie|
browser.cookies.add(saved_cookie[:name], saved_cookie[:value])
end

How to backup browser state after Watir automation

I finally have a solution!

It appears that watir-webdriver stores the browser state/user data in random path. By default this can be found here (where XXXXXX is the random identifier):

/private/var/folders/2v/vkd2v3vs5njf69m59nqzc16m0000gn/T/.com.google.Chrome.XXXXXX/Default/

Instead of relying on this default and randomized path, you can specify a precise location for the user data using the following flag:

Watir::Browser.new :chrome, :switches => %w[--user-data-dir=/path/to/user/data]

Then the cache, cookies, etc. can be backed up, deleted, etc. using Ruby's standard library. Hopefully this helps someone else.

Edit: If you are unable to find where watir-webdriver is storing your user data by default, find Chrome's process id by running watir-webdriver and top. Once you have the pid, type lsof -p <pid> into terminal to find the path to the user data.

Python - Start firefox with Selenium in private mode

Referring to the @Laas's point at How might I simulate a private browsing experience in Watir? (Selenium):

Selenium is equivalent to turning on Private Browsing.

And the definition of "Private Browsing":

Private Browsing allows you to browse the Internet without saving any
information about which sites and pages you’ve visited.

And since every time you start firefox through selenium webdriver it creates a brand new anonymous profile, you are actually browsing privately.


If you still want to force the private mode in Firefox, set the browser.privatebrowsing.autostart configuration option to true:

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.privatebrowsing.autostart", True)

driver = webdriver.Firefox(firefox_profile=firefox_profile)

Also, see:

  • Python/Selenium incognito/private mode

Watir and Javascript

Tim provides a pretty good answer.

The only thing I have to add to what he said is that I've found that now and then I have to use the watir methods to fire specific javascript events such as onmouseover in order to accurately simulate the user interacting with the page. Since watir has a method for this, the hard part is not the watir code, but reverse engineering the page (or noticing subtle page interactions based on user actions) to figure out what elements are 'wired' up to what events and the order to fire those events against those specific elements.

Usually it's pretty easy to look at the HTML for an element and see what's going on. But with some custom controls it can take a bit of learning because they manage to do a pretty good job of 'hiding' all the event wiring, and you may have to parse through various aspects of the page (styles and all) using something like fiddler.

(after all, the normal user will never 'force' javacript to execute, or 'inject' javascript. They will use the mouse and keyboard to interact with the page, and any javascript is going to be a result of scripts that execute when the page is loaded, or as a result of scripts triggered via events based on specific user actions)



Related Topics



Leave a reply



Submit