Open Incognito mode in selenium 4 in java - selenium

I wanted to open a normal and incognito mode together in selenium. I could open two browsers in normal mode but I am not sure how to open the another open in incognito mode in selenium 4.
The below open the 2nd browser window in normal mode where I want this to be opened in incognito mode.
driver.switchTo().newWindow(WindowType.WINDOW).get("URI");
Expected:
1st browser window in normal mode.
2nd browser window in incognito mode.
Actual:
1 browser opened in normal mode.
2nd browser opened in normal mode.

WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
WebDriver driver_1 = new ChromeDriver();
driver_1.manage().window().maximize();
driver_1.get("url");
WebDriver driver_2 = new ChromeDriver(options);
driver_2.manage().window().maximize();
driver_2.get("url");

That's one of the special abilities of https://github.com/seleniumbase/SeleniumBase
Here's a test you can run with pytest after doing pip install seleniumbase:
from seleniumbase import BaseCase
class MultipleDriversTest(BaseCase):
def test_multiple_drivers(self):
self.open("data:text/html,<h1>Driver 1</h1>")
driver2 = self.get_new_driver(incognito=True)
self.open("data:text/html,<h1>Driver 2</h1>")
self.switch_to_default_driver() # Driver 1
self.highlight("h1")
self.assert_text("Driver 1", "h1")
self.switch_to_driver(driver2) # Driver 2
self.highlight("h1")
self.assert_text("Driver 2", "h1")
Driver 1 will be regular Chrome. Driver 2 will be incognito Chrome. It easily switches between the two.

Related

Python Selenium - open more Chrome apps

I'm using Selenium to automate certain stuff in Chrome and I know how to open multiple tabs, but is it possible to open Chrome itself multiple times?
Right now, when I want to open a new Chrome app the old one closes. I want it to stay open.
every time you want to open a new Chrome Browser you have to create a new instance of the webdriver.
from time import sleep
from selenium import webdriver
fist_driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
fist_driver.get("https://google.com")
second_driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
second_driver.get("https://ifconfig.me")
sleep(5)
# using for loop
for _ in range(2): # How much browser you want to open
driver = webdriver.Chrome(executable_path="/path/to/driver")
driver.get("https://google.com")
sleep(5)

Cannot use console in Chrome Dev tools in the Chrome window controlled by selenium chrome driver

I can use console in Chrome Dev Tools in a normal Chrome window to debug xpath like $x("//div") to debug the xpath.
But when Selenium-Chrome-Driver generates a Chrome window in C# SpecFlow, in this Chrome window, I cannot debug xpath in console by typing $x("//div"), the console reported error:
$x("//div")
VM1819:1 Uncaught TypeError: $x is not a function
at :1:1
Already tried the following code to switch on some ChromeOptions, but got the same problem.
ChromeOptions options = new ChromeOptions();
string user_data_dir = settings.SelectSingleNode("//LoginEmail/ChromeUserDataDir").InnerText;
user_data_dir = String.Format(#"user-data-dir={0}", user_data_dir);
options.AddArgument(user_data_dir);
options.AddArgument(#"--enable-devtools-experiments");
options.AddArgument(#"--auto-open-devtools-for-tabs");
webDriver = new ChromeDriver(options);
I have found another way to debug xpath in the console of dev tools in chrome window controlled by chromedriver in specflow
that is using javascript:
document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.outerHTML
for example:
document.evaluate("//a[contains(#href, 'mail.google.com') and contains(#href,'#drafts')]/../../div[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.outerHTML

URL is not getting typed in browser while loading Chrome with Extension in Node Machine in Selenium Grid Automation

While running Selenium Grid script on node with extension in chrome, the given URL is not getting typed in browser bar.
Test Condition: Below steps is been automated.
Open chrome browser with Multipass chrome extension on node.
Entering Extension Option html page in browser bar for filling some values in extension page.
Navigating to Test Application URL.
Output: Chrome is getting loaded in node machine with extension, but after that Extension option html page link URL is not getting typed in bar. Finally script get timed out exception.
Selenium Server: Latest version
Chrome Driver: 74…
Chrome: 74..
Note Points: Script work fine if we make same machine as hub and node. Also the above script in node will work fine if we comment out the add extension statement in script.
Code:
public void Launch () {
//Setting capabilities as per the node
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WIN10);
ChromeOptions options = new ChromeOptions ();
options.addArguments("start-maximized");
//Adding extension in chrome on node with CRX file of extension
options.addExtensions (newFile("src\\test\\resources\\MultiPass-for-HTTP-basic-authentication_v0.8.4.crx"));
options.merge(capabilities);
//opening driver with hub url details and capabilities options for node
driver = newRemoteWebDriver(new URL("http://10.141.108.84:4444/wd/hub"),options);
wait = newWebDriverWait(driver,130);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
//opening the extension option html page for entering some values in shown page
driver.get("chrome-extension://enhldmjbphoeibbpdhmjkchohnidgnah/options.html");
}

Can't run Chrome browser with custom profile selenium

I'm trying start selenium tests in chrome browser with my custom Profile which contains necessary cookies.
I use Chrome 57 and chromedriver 2.29
My code is :
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches", "--disable-extensions");
options.addArguments("user-data-dir=/Users/tester/Desktop/ChromeProf/QAChromeProfile");
options.addArguments("--ignore-certificate-errors","disable-infobars","enable-automation","--window-size=375,667");
WebDriver driver = new ChromeDriver(options);
It works fine, but don't use my chrome profile. Help me, pls...)
Don't include the profile directory in the user-data-dir argument. When using user-data-dir option with any profile besides the default, i've had to also use the profile-dir argument.
Make sure you change user-data-dir to your Chrome User Data directory. Don't worry about spaces. Do not try to put quotes or to escape spaces in the argument value.
Profile 1
from selenium.webdriver import Chrome, ChromeOptions
options = ChromeOptions()
options.add_argument("user-data-dir=C:/Users/<username>/AppData/Local/Google/Chrome/User Data")
options.add_argument("profile-directory=Profile 1")
driver = Chrome(executable_path=r'C:/path/to/chromedriver.exe', chrome_options=options)
driver.get("https://www.google.com")
Keep this in mind when running your code. (basically, close any open Chrome instances running before running selenium with custom Chrome profiles in use)

Selenium chromedriver won't launch URL if another chrome instance is open

I tried to load chrome profile using selenium weDriver. The profile loads fine but it failed when it tries to load the URL.
I noticed that this issue happens when there is another chrome instance open whether or not it was open by webDriver. I have selenium 2.53.1.
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/useName/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("www.google.com") // here is where it fails. It works fine if I close all chrome browsers before I run the test
I found a workaround for this issue. I noticed that this issue happens because chromedriver will not be able to launch with the same profile if there is another open instance using the same profile. For example, if chrome.exe is already open with the default profile, chromedriver.exe will not be able to launch the default profile because chrome.exe is already open and using the same profile.
To fix this, you will need to create a separate profile for automation by copying the default profile so that chromedriver.exe and chrome.exe don't share the same default profile.
The default chrome profile is in this location:
C:\Users\yourUserName\AppData\Local\Google\Chrome\User Data\
Copy all files from User Data folder to a new folder and call it AutomationProfile
After you copy the files to the new folder then you can use it for your scripts.
String userProfile= "C:\\Users\\YourUserName\\AppData\\Local\\Google\\Chrome\\AutomationProfile\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
Make sure you use driver.quit() at the end of your test so that you don't keep chromedriver.exe open
I added the ChromeOption "no-sandbox", and it seemed to help me with a similar issue. Know that this changes how secure your browsing can be. Here's a link that explains it more: https://www.google.com/googlebooks/chrome/med_26.html
var options = new ChromeOptions();
//I had more options added, but this is the example of the argument I referred to
options.AddArgument("no-sandbox");