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

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");

Related

Is there a way that you can use the browser profiles on your desktop as the Selenium webdriver? [duplicate]

So whenever I try to use my Chrome settings (the settings I use in the default browser) by adding
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)
it shows me the error code
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape
in my bash. I don't know what that means and I'd be happy for any kind of help I can get. Thanks in advance!
The accepted answer is wrong. This is the official and correct way to do it:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\path\to\chrome\user\data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
options.add_argument(r'--profile-directory=YourProfileDir') #e.g. Profile 3
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
To find the profile folder on Windows right-click the desktop shortcut of the Chrome profile you want to use and go to properties -> shortcut and you will find it in the "target" text box.
To get the path, follow the steps below.
In the search bar type the following and press enter
This will then show all the metadata. There find the path to the profile
As per your question and your code trials if you want to open a Chrome Browsing Session here are the following options:
To use the default Chrome Profile:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
Note: Your default chrome profile would contain a lot of bookmarks, extensions, theme, cookies etc. Selenium may fail to load it. So as per the best practices create a new chrome profile for your #Test and store/save/configure within the profile the required data.
To use the customized Chrome Profile:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
Here you will find a detailed discussion on How to open a Chrome Profile through Python
This is how I managed to use EXISTING CHROME PROFILE in php selenium webdriver.
Profile 6 is NOT my default profile. I dont know how to run default profile. It is IMPORTANT not to add -- before chrome option arguments! All other variants of options didnt work!
<?php
//...
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments([
'user-data-dir=C:/Users/MyUser/AppData/Local/Google/Chrome/User Data',
'profile-directory=Profile 6'
]);
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
$driver = RemoteWebDriver::create($host, $capabilities, 100000, 100000);
To get name of your chrome profile, go to chrome://settings/manageProfile, click on profile icon, click "Show profile shortcut on my desktop". After that right click on desktop profile icon and go to properties, here you will see something like "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 6".
Also I recommend you to close all chrome instances before running this code. Also maybe you need to TURN OFF chrome settings > advanced > system > "Continue running background apps when Google Chrome is closed".
None of the given answers were working for me so I researched a bit and now the working code is for is this one. I copied the user dir folder from Profile Path from chrome://version/ and made another argument for the profile as shown below:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\gupta\\AppData\\Local\\Google\\Chrome\\User Data')
options.add_argument('profile-directory=Profile 1')
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe', options=options)
driver.get('https://google.com')
Are you sure you are meant to be putting in the webdriver path in the user-data-dir argument? That's usually where you put your chrome profile e.g. "C:\Users\yourusername\AppData\Local\Google\Chrome\User Data\Profile 1\". Also you will need to use either double backslashes or forward slashes in your directory path (both work). You can test if your path works by using the os library
e.g.
import os
os.list("C:\\Users\\yourusername\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1")
will give you the directory listing.
I might also add that occasionally if you manage to crash chrome while running webdriver with a nominated user profile, that it seems to record the crash in the profile and the next time you open chrome, you get the Chrome prompt to restore pages after it exited abnormally. For me personally this had been a bit of headache to deal with and I no longer use a user profile with chromedriver because of it. I could not find a way around it. Other people have reported it here, but none of their solutions seemed to work for me, or were not suitable for my test cases. https://superuser.com/questions/237608/how-to-hide-chrome-warning-after-crash
If you don't nominate a user profile it seems to create a new (blank) temporary one each time it runs
Make sure you've got the path to the profile right, and that you double escape backslashes in said path.
For example, typically the default profile on windows is located at:
"C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default"
I managed to launch my chrome profile using these arguments:
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=Profile 2");
WebDriver driver = new ChromeDriver(options);
You can find out more about the web driver here
You simply have to replace the '\' to '/' in your paths and that'll resolve it.
Get profile name by navigating to chrome://version from your chrome browser (You'll see Profile Path, but you only want the profile name from it (e.g. Profile 1)
Close out all Chrome sessions using the profile you want to use. (or else you will get the following error: InvalidArgumentException)
Now make sure you have the code below (Make sure you replace UserFolder with the name of your userfolder.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\EnterYourUserFolder\\AppData\\Local\\Google\\Chrome\\User Data") #leave out the profile
options.add_argument("profile-directory=Profile 1") #enter profile here
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
this worked for me 100% and it showed up my selected profile.
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# path of your chrome webdriver
dir_path = os.getcwd()
user_profile_path = os.environ[ 'USERPROFILE' ]
#if "frtkpr" which is ll be your custom profile does not exist it will be created.
option.add_argument( "user-data-dir=" + user_profile_path + "/AppData/Local/Google/Chrome/User Data/frtkpr" )
driver = webdriver.Chrome( dir_path + "/chromedriver.exe",chrome_options=option )
baseUrl = "https://www.facebook.com/"
driver.maximize_window()
driver.get( baseUrl )

How to install extension permanently in geckodriver

I need to test Firefox using an extension. I want to automate the test and visit several websites.
I installed Selenium and it opens in geckodriver. However, the extension is not there. I can install it manually from about:debugging but the problem is that I want the Selenium test to launch the gecko driver while the extension is already there. How to do this? How to install the extension permanently in the geckodriver so it is there when I launch the geckodriver from selenium?
EDIT:
I also tried to install the extension (add it to the browser) from the Firefox extensions websites. It gets added but once I close the gecko window, the extension disappear in the next run. How to install it permanently?
Note: OP didn't specify a language, so this answer is for Python. The other Selenium WebDriver language bindings have similar mechanisms for creating profiles and adding extensions.
You can install the Extension each time you instantiate the driver.
First, download the extension (XPI file) you want from: https://addons.mozilla.org.
Then, in your code... create a FirefoxProfile() and use the add_extension() method to add the extension. Then you can instantiate a driver using that profile.
For example, this will launch Firefox with a newly created profile containing the "HTTPS Everywhere" extension:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.add_extension(extension='https_everywhere-2019.1.31-an+fx.xpi')
driver = webdriver.Firefox(firefox_profile=profile)
You need to launch geckdriver with an exisitng profile by specifying the profile path of firefox
For python you can do it by this:
profile = FirefoxProfile('/home/student/.mozilla/firefox/gwi6uqpe.Default') // change this path
browser = webdriver.Firefox(firefox_profile=profile)
For C# you can do this:
string path = #"C:\Users\username\AppData\Local\Mozilla\Firefox\Profiles\myi5go1k.default";
FirefoxProfile ffprofile = new FirefoxProfile(path);
Driver = new FirefoxDriver(ffprofile);
I found that this was warking for me:
from selenium import webdriver
driver_path = r"G:\Libs\geckoDriver\firefox\geckodriver.exe"
driver = webdriver.Firefox(executable_path=driver_path)
path = r"G:\Libs\ext\uBlock0_1.38.7b5.firefox.signed.xpi"
driver.install_addon(path, temporary=True)
driver.profile = webdriver.FirefoxProfile()
driver.profile.add_extension(path)
driver.profile.set_preference("security.fileuri.strict_origin_policy", False)
driver.profile.update_preferences()`enter code here`
Reference:
[Python] https://cyruslab.net/2020/08/26/python-adding-extension-to-geckodriver-with-selenium/
You can install an extension/addon permanently within a specific Firefox Profile and use it. To achieve that you need follow the below mentioned steps:
You need to create a new Firefox Profile manually (e.g. FirefoxExtensionProfile) following the instructions at Creating a new Firefox profile on Windows.
Open a Firefox Browsing session manually and invoke the url https://addons.mozilla.org/en-US/firefox/
In the Search Box search for an extension e.g. HTTPS Everywhere.
Click on the search result and install / enable (incase previously installed and currently disabled) the extension.
Now you can use the following Java solution to open the Firefox Profile FirefoxExtensionProfile containing the extension HTTPS Everywhere
Code Block:
package A_MozillaFirefox;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
public class A_FirefoxProfile_dc_opt {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("FirefoxExtensionProfile");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com");
}
}
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
[Python] How to load extension within chrome driver in selenium with python
[Python] How to install Chrome Extension using Selenium & Python

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)

Jmeter with selenium webdriver plugin-start firefox session without cleaning cookies/cache

I am using Jmeter 2.13 alongside with a selenium webdriver plugin. When I start my thread it opens a new firefox session with all the cookies and cache cleared. In a previous session I have made a sync that lasts almost 5 minutes that brings me in my app some products. I don`t want to sync everytime I start the thread.
Is there any way I could start the new firefox session without clearing cookies/cache ?
If my guess is correct, that Selenium is the one that is opening an instance of Firefox, please see below. If not, please provide more info about what you have setup with code examples.
By default Selenium is opening the Firefox in Safe mode, where a lot of stuff are disabled (like extensions, localstore settings, etc). This also means that you won't have any cache or cookies.
You can read more about this here.
To disable this, you need to set the `toolkit.startup.max_resumed_crashes` setting key in in `about:config` to `-1`.
Selenium code wise, this can be achieved by setting the preference in the FirefoxProfile. C# code to achieve it would look like this:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.SetPreference("toolkit.startup.max_resumed_crashes", "-1");
IWebDriver driver = new FirefoxDriver(firefoxProfile);
Looking into FirefoxDriverConfig.java source code the plugin creates new profile each time Firefox starts:
FirefoxProfile createProfile() {
FirefoxProfile profile = new FirefoxProfile();
String userAgentOverride = getUserAgentOverride();
String ntlmOverride = getNtlmSetting();
if (StringUtils.isNotEmpty(userAgentOverride)) {
profile.setPreference("general.useragent.override", userAgentOverride);
}
if (StringUtils.isNotEmpty(ntlmOverride)) {
profile.setPreference("network.negotiate-auth.allow-insecure-ntlm-v1", true);
}
profile.setPreference("app.update.enabled", false);
addExtensions(profile);
setPreferences(profile);
return profile;
}
So there are 2 options:
Get plugin source code and amend profile initialisation line to use your existing profile as:
FirefoxProfile profile = new FirefoxProfile("/path/to/firefox/profile");
See How do I find my profile page of Mozilla documentation for instructions on how you can locate your current profile directory.
Stop using WebDriver Sampler and switch to JSR223 Sampler instead, it supports all the languages WebDriver Sampler does and provides full control (you'll have to write all the code to configure, start and stop browser yourself)

How to create profile in Firefox using Selenium WebDriver

When we write something like this:
FirefoxProfile ffprofile = new FirefoxProfile(new File("D:\\Selenium"));
Does it mean we are creating a new Profile? Because I won't be able to find any new profile in the Firefox Profile section.
So now my question is, how do I create a new profile for a Firefox browser?
The method call you stated simply creates a java profile object from the given directory of profile information which is then passed to Firefox via the WebDriver instance.
In order to get Firefox to persist your driver and make it available from profile manager, you need to edit the file profiles.ini, on my (Windows 7) machine this was in:
%APPDATA%\Roaming\Mozilla\Firefox
The Profiles directory within this folder contains the stores of existing Firefox profiles, which are quite handy to copy when you want to use an existing profile as the template for a new one.
Your mileage may vary depending on your OS, but I'm sure you can find it with a quick search. Using your example, you'd then add the following to this file (where N in the header is the next unused profile number):
[ProfileN]
Name=selenium
IsRelative=0
Path=D:\Selenium
This will cause Firefox Profile Manager to load the profile and will allow you to then launch Firefox manually with this profile to configure or test it, which is what I presume you want to do.
Once you have created a named profile in this way, you can assign it to your driver in Selenium like this:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
WebDriver driver = FirefoxDriver(profile);
Where "selenium" is the same as the Name property in the profiles.ini file.
You cannot create a profile for firefox using Selenium. What you can do is create a firefox profile for your webdriver from the available profiles in firefox. Firefox profile word sounds bit ambiguous here.
To create a firefox profile in browser, refer Mozilla support page for details.
Following code will create firefox profile (based on provided file) and create a new FF webdriver instance with this profile loaded:
FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile"));
WebDriver driver = new FirefoxDriver(profile);
Maybe take a look on the official support page for FF profile manager
or here: Custom Firefox profile for Selenium to get some idea on FF profiles.
Here is how I do by selenium 3 with geckodriver:
Use firefox command line interface to create profile
firefox.exe -CreateProfile "profile_name profile_dir"
(In java, execute this runtime by Runtime.getRuntime().exec function)
Set -profile argument in firefox options
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-profile", <profile_dir>);
driver = new FirefoxDriver(options);
Create profile in Firefox browser.
Here is the code for using newly created firefox profile.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("firefox profile name");
WebDriver driver = new FirefoxDriver(myprofile);