Python/Selenium incognito/private mode - python

I can not seem to find any documentation on how to make Selenium open the browser in incognito mode.
Do I have to setup a custom profile in the browser or?

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:
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:

Note: chrome_options is now deprecated. We can use 'options' instead of chrome_options
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
driver = webdriver.Chrome(options=options)
driver.get('https://google.com')

I have initiated both Chrome and Firefox in incognito/Private mode using ChromeOptions and FirefoxOptions successfully using the code snippets in Java as below:
//For Firefox
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-private");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("moz:firefoxOptions",options);
//For Chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("-incognito");
caps.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);

There is a really simple way to make a window open in incognito mode:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# incognito window
chrome_options.add_argument("--incognito")
You can also use this library for maximizing the window and more, see the documentation: https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html

For firefox : (Python) ==>
from selenium import webdriver
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument("--private")
browser = webdriver.Firefox(firefox_options=firefox_options)

//We need to add argument "--incogneto" in ChromeOptions object and pass this ChromeOptions instance to the web driver initialization.
ChromeOptions options = new ChromeOptions()
options.addArgument("start-maximized");
options.addArgument("--incognito");
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://investopedia.com");

In Chrome Browser You Can Do This Using Python As Follows
As you can see when you uses chrome, you have the option of incognito mode in the options menu part of the chrome browser. So when you are using selenium, you can alter the things of options using
chrome_options = webdriver.ChromeOptions()
So, the code is:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(executable_path="<path of chrome_driver.exe file>",options=chrome_options)
So the only thing you have to do is to give "webdriver.Chrome" this given value to its another parameter i.e. "options".

For python with opera
from selenium import webdriver
options = webdriver.opera.webdriver.Options()
options.add_argument("private")
driver = webdriver.Opera(executable_path="operadriver",options=options)

PowerShell
try{
# Import the Selenium DLLs
Add-Type -Path "$Seleniumlib\Selenium.WebDriverBackedSelenium.dll"
Add-Type -Path "$Seleniumlib\WebDriver.dll"
Add-Type -Path "$Seleniumlib\WebDriver.Support.dll"
}
catch [Exception]{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
$options = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$options.AddArgument("--incognito")
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($options)

Some options have been deprecated so for Firefox it worked out for me like this:
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
firefox_options = Options()
firefox_options.add_argument("-private")
driver = webdriver.Firefox(options=firefox_options)

Related

Chrome browser automatically closing just after 1 sec,, what would be the reason

from selenium import webdriver
driver= webdriver.Chrome(executable_path="C:\Drivers\chromedriver_win32\chromedriver.exe")
driver.get("https://opensource-demo.orangehrmlive.com")
just opens the chrome and closes automatically after 1 sec
what is the reason? is this any error with code?
Try using the detach option when initializing the chromedriver. Also, executable_path is deprecated now. Service should be used instead, as following:
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
webdriver_service = Service('C:\Drivers\chromedriver_win32\chromedriver.exe')
driver = webdriver.Chrome(options=chrome_options, service=webdriver_service)

Pass Options object with option kwargs in BrowserStack [duplicate]

The Selenium documentation mentions that the Chrome webdriver can take an instance of ChromeOptions, but I can't figure out how to create ChromeOptions.
I'm hoping to pass the --disable-extensions flag to Chrome.
Found the chrome Options class in the Selenium source code.
Usage to create a Chrome driver instance:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)
This is how I did it.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')
chrome = webdriver.Chrome(chrome_options=chrome_options)
Code which disable chrome extensions for ones, who uses DesiredCapabilities to set browser flags :
desired_capabilities['chromeOptions'] = {
"args": ["--disable-extensions"],
"extensions": []
}
webdriver.Chrome(desired_capabilities=desired_capabilities)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--disable-logging')
# Update your desired_capabilities dict withe extra options.
desired_capabilities.update(options.to_capabilities())
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())
Both the desired_capabilities and options.to_capabilities() are dictionaries. You can use the dict.update() method to add the options to the main set.

Is there a way to hide the browser while running selenium in Python?

I am working on a project with selenium to scrape the data, but I don't want the browser to open and pop up. I just wanted to hide the browser and also not to display it in the taskbar also...
Some also suggested to use phantomJS but I didn't get them. What to do now ...
If you're using Chrome you can just set the headless argument like so:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
driver_exe = 'chromedriver'
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(driver_exe, options=options)
For chrome you could pass in the --headless parameter.
Alternatively you could let selenium work on a virtual display like this:
from selenium import webdriver
from xvfbwrapper import Xvfb
display = Xvfb()
display.start()
driver = webdriver.Chrome()
driver.get('http://www.stackoverflow.com')
print(driver.title)
driver.quit()
display.stop()
The latter has worked for me quite well.
To hide the browser while executing tests using Selenium's python you can use the minimize_window() method which eventually minimizes/pushes the Chrome Browsing Context effectively to the background using the following solution:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()
Alternative
As an alternative you can use the headless attribute to configure ChromeDriver to initiate google-chrome browser in Headless mode using Selenium and you can find a couple of relevant discussions in:
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
If you're using Firefox, try this:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
driver_exe = 'path/to/firefoxdriver'
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(driver_exe, options=options)
similar to what #Meshi answered in case of Chrome
if you want to hide chrome or selenium driver there is a library pyautogui
import pyautogui
window = [ x for x in pyautogui.getAllWindows()]
by this, you are getting all window title
now you need to find your window
for i in window:
if 'Google Chrome' in i.title:
i.hide()
or you can play with your driver title also

How to bypass the message-"your connection is not private" on non-secure page using Selenium?

I'm trying to interact with the page "Your connection is not private".
The solution of using options.add_argument('--ignore-certificate-errors') is not helpful for two reasons:
I'm using an already open window.
Even if I was using a "selenium opened window" the script runs non stop, and the issue I'm trying to solve is when my browser disconnects from a splunk dashboard and I want it to automatically connect again(and it pops the private connection window).
How do I click on "Advanced" and then click on "Proceed to splunk_server (unsafe)?
For chrome:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)
If not work then this:
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
options = webdriver.ChromeOptions()
options.add_argument('--allow-insecure-localhost') # differ on driver version. can ignore.
caps = options.to_capabilities()
caps["acceptInsecureCerts"] = True
driver = webdriver.Chrome(desired_capabilities=caps)
For firefox:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://cacert.org/')
driver.close()
If not work then this:
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities['acceptSslCerts'] = True
driver = webdriver.Firefox(capabilities=capabilities)
driver.get('https://cacert.org/')
driver.close()
Above all worked for me!
This is how i handle this problem:
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
ChromeOptions capability = new ChromeOptions();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true);
WebDriver driver = new ChromeDriver(capability);
This chrome option is the silver bullet for me:
chromeOptions.addArguments("--allow-running-insecure-content");
If you need more, Open chrome & paste this URL:
chrome://flags/
One will find all the options and their impact on the chrome.
Either of below 2 solutions worked for me using Python Chrome Selenium Webdriver:
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
capabilities = DesiredCapabilities.CHROME.copy()
capabilities["acceptInsecureCerts"] = True
driver = webdriver.Chrome(desired_capabilities=capabilities)
And accepted solution:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)

How do I pass options to the Selenium Chrome driver using Python?

The Selenium documentation mentions that the Chrome webdriver can take an instance of ChromeOptions, but I can't figure out how to create ChromeOptions.
I'm hoping to pass the --disable-extensions flag to Chrome.
Found the chrome Options class in the Selenium source code.
Usage to create a Chrome driver instance:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)
This is how I did it.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')
chrome = webdriver.Chrome(chrome_options=chrome_options)
Code which disable chrome extensions for ones, who uses DesiredCapabilities to set browser flags :
desired_capabilities['chromeOptions'] = {
"args": ["--disable-extensions"],
"extensions": []
}
webdriver.Chrome(desired_capabilities=desired_capabilities)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--disable-logging')
# Update your desired_capabilities dict withe extra options.
desired_capabilities.update(options.to_capabilities())
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())
Both the desired_capabilities and options.to_capabilities() are dictionaries. You can use the dict.update() method to add the options to the main set.

Categories