I am facing an issue with my automation script.I am logging into a URL in my script.When I do that manually it login normally but when I use automation script it asks for verification code which is available on my mail.
I thought if I will use the chrome default profile for login then this issue should not arise.But it didn't helped me out.
Can anyone suggest any solution ?
As known, Webdriver always starts with fresh, default profile. That is the reason its asking for verification but not same when do manually. To avoid it, you can specify the chrome profile which is used manually to webdriver.
In Java, we can done it by using ChromeOptions and Chrome Profile. In chrome navigate to chrome://version/ It will display profile path and Executable path.
As per my working on this, \Local\Google\Chrome\User Data\Profile 3 is displaying when navigate to chrome://version/ in normal chrome. In this profile, i navigated to stackoverflow and saved credentials. So used below code
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("binary", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
System.setProperty("webdriver.chrome.driver", "E:\\selenium_setups\\poi-3.12\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("user-data-dir=C:\\Users\\murali\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 3");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
//WebDriver driver = new ChromeDriver(options);
driver.get("http://stackoverflow.com/");
As per my understanding, i excepted stackoverflow.com page displayed as logged in. but for first time, i am not logged in. so cross checked with chrome://version/ in chrome opened by driver, profile path is displayed as
\Local\Google\Chrome\User Data\Profile 3\Default . then logged manually in that profile it self, which is opened by webdriver and executed gain by closing it.
Finally, page is displayed as logged in. So it may be in java, i hope it will helps you.
Thank You,
Murali
Related
For background, I'm trying to log-in to a web site using Selenium. However, I can't seem to navigate through the log-in page of the OAuth provider - in this case, MS. Even when I've managed it using various techniques of waiting, and so forth, I get stuck in 2FA.
As a result, I've now decided that what I need to do is to log-in to the site manually, and then run the Selenium script. However, I can't seem to find anywhere that shows how to connect to an existing broser instance:
var webDriver = new ChromeDriver();
Just launches a new instance. Is there a way to attach to an existing instance; or, is there a way to navigate an external OAuth provider, such as B2C?
Start chrome as :
C:/pathtochroome/chrome.exe --remote-debugging-port=1559
Add connect to it as:
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("debuggerAddress", "127.0.0.1:1559")
if this doesn't work try:
ChromeOptions options = new ChromeOptions();
options.DebuggerAddress = "127.0.0.1:1559";
Though I realize it's NOT "good" practice - I have a use case where I need to point (hook up) the Selenium driver to my default Chrome session/profile.
My default profile is here:
~/Library/Caches/Google/Chrome/Default
Here is how I'm seting it up currently: (not working)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Caches/Google/Chrome")
options.add_argument("--profile-directory=Default")
browser = webdriver.Chrome(options=options, executable_path=r"./chromedriver")
browser.get("http://google.com")
I'm using Chrome version 74.0.3729.169 and chromedriver version ChromeDriver 74.0.3729.6 (which is the compatible version).
When Chrome opens I don't see any cookies in Chrome's settings so it's clear it's NOT being pointed to my default session. Also, I see that a Selenium directory has been created (which appears to mean that it has failed to connect to the session at ~/Library/Caches/Google/Chrome/Default.
How do I hook up selenium to my default Chrome session? This is the same session as one sees when normally opening up Chrome.
I've looked at this other question, but the answer there fails to address how to point Selenium towards default session. Also - it's an outdated question - Chrome and Chromedriver have progressed a lot since then. Also, the question there assumes that the poster is able to connect to default session - I am not able to do that which suggests that the Chromedriver/Chrome have changed since then. Also that question is for Windows - I'm on a Mac where things work differently.
Make sure you are pointing to the right folder using "Chrome://version".
I am using the windows but it should be similar in you mac case too.
Refer to this link for more information.
How to create a custom profile:
You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the user-data-dir switch set to some new directory. If the path doesn't exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open chrome://version in the browser to see what profile Chrome is using.
Reference:
http://chromedriver.chromium.org/capabilities
To start with, No, you can't point (hook up) the Selenium driver to any of the existing/previous Web Browsing session. Even if you are able to extract the Session ID, Cookies and other session attributes from the existing/previous Web Browsing session, still you won't be able to pass those attributes as a HOOK to the WebDriver.
You can find a detailed discussion in How can I reconnect to the browser opened by webdriver with selenium?
But of coarse you can connect to the existing Default Chrome profile.
You seem to be already aware that trying to use the Default Chrome Profile for Test Automation will be against all the best practices as the Default Chrome Profile may contain either/all of the following:
browser settings
Extensions
Bookmarks
Apps
Saved Passwords
Browsing History
etc
So the Default Chrome Profile may not be in compliance with you Test Specification and may occasionally raise exception while trying to load. Hence you should always use a customized Chrome Profile.
You can find a detailed discussion in How to open a Chrome Profile through --user-data-dir argument of Selenium
If your usecase still warrants to use the Default Chrome Profile you need to follow the below mentioned details.
Location of Default Chrome Profile
As per the documentation in How to Find Your Chrome Profile Folder on Windows, Mac, and Linux the location for Chrome’s default profile folder differs depending on your platform. The locations are:
Windows 7, 8.1, and 10: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
Mac OS X El Capitan: Users/<username>/Library/Application Support/Google/Chrome/Default
Linux: /home/<username>/.config/google-chrome/default
You need to replace <username> with the name of your user folder. The default profile folder is simply named Default (or default in Linux). However, if you’ve created additional profiles, their folder names are not as obvious. The name you assigned to the profile when you created it displays on a name button on the right side of the title bar on the Chrome window. Unfortunately, the name Chrome uses on the associated profile folder is a generic, numbered name like Profile 3.
If you need to know any of the Chrome Profile's folder name, you simply need to access chrome://version in the address bar and press Enter.
Snapshot:
The Profile Path shows the location of the current profile. For example, the location of my Default profile in my Windows 10 system is C:\Users\Soma Bhattacharjee\AppData\Local\Google\Chrome\User Data\Default. You can select the path and copy it and paste it into File Explorer in Windows, the Finder on OS X or into a file manager like Nautilus in Linux to access that folder.
Sample Code (Windows 10)
Finally, to access the Default Chrome Profile you can use the following Python based solution:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\Soma Bhattacharjee\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
You can find a detailed discussion in How to use Chrome Profile in Selenium Webdriver Python 3
I am testing my site.
I want the chrome browser to go headless after I manually enter the login credentials.
My selenium code to connect to the website.
var driverService = ChromeDriverService.CreateDefaultService();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--start-maximized");
chromeOptions.AddArgument("no-sandbox");
//chromeOptions.AddArgument("--headless");
driver.Navigate().GoToUrl("exampleDOTcom");
while (true)
{
Console.WriteLine("Login and Press Enter");
Console.ReadLine();
if (CheckLoggedIn())
break;
}
//driver = new ChromeDriver(driverService, chromeOptions,
TimeSpan.FromSeconds(180));
chromeOptions.AddArgument("--headless");
No, it won't be possible to make Chrome operate headlessly after you login manually.
When you configure an instance of a ChromeDriver using ChromeOptions() or DesiredCapabilities() in the process of initiating a new Chrome Browsing Session the configuration gets baked into the chromedriver executable and will persist till the lifetime of the WebDriver and being uneditable. So you can't add any further ChromeOptions to the WebDriver instance which is currently in execution.
Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies, UserAgent and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won't be able to change the set of attributes of the ChromeDriver.
A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the current ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.
tl; dr
You can find a couple of relevant discussions in:
How to set selenium webdriver from headless mode to normal mode within the same session?
Change ChromeOptions in an existing webdriver
If you need headless browser, Please Specify before the browser opening. i Haven't Seen any applications doing that activity
Things to pay attention to before using headless browser:
Headless browsers are simulation programs, they are not your real browsers. Most of these headless browsers have evolved enough to simulate, to a pretty close approximation, like a real browser. Still you would not want to run all your tests in a headless browser. JavaScript is one area where you would want to be really careful before using a Headless browser. JavaScript are implemented differently by different browsers. Although JavaScript is a standard but each browser has its own little differences in the way that they have implemented JavaScript. This is also true in case of headless browsers also. For example HtmlUnit headless browser uses the Rihno JavaScript engine which not being used by any other browser.
Here is Article = https://developers.google.com/web/updates/2017/04/headless-chrome
Check it out!
If I open my Chrome browser, it has all of the cookies from pages, what I used. I'd like to enable this cookies in ChromeDriver. So if I open for example facebook.com, I don't want to enter my username and my password, if I did that earlier.
Anyway, I want webdriver to use my cookies from my PC.
or
How can I get all of the cookies from chrome's folder, to add to the webdriver? Theoretically I can add cookies to webdriver.
You have to make a new instance from ChromeOptions, and add the following argument:
ChromeOptions options = new ChromeOptions();
options.AddArguments(#"--user-data-dir=C:\Users\your username\AppData\Local\Google\Chrome\User Data");
after that, you shold add this options instance to your ChromeDriver, or iWebDriver, and of course it works with FirefixDriver, and so on...
ChromeDriver driver = new ChromeDriver(driverPath, options);
So you can set, that your driver get data from your browser's User Data folder. Theoretically it works with every browser driver, just you have to search your browser's data folder.
I am trying to use selenium with .NET (C#) to pull Flash cookies from a particular URL (in this case, a game on kongregate). I know where the flash cookies are stored physically for the browser I use personally. I attempted to access the flash cookies using the physical location in google Chrome's directory (Google\Chrome\User Data\Default\Pepper Data\Shockwave Flash\WritableRoot#SharedObjects), however this has no effect on the flash cookies a gather using selenium. How can I get access to the Shared Object files that are stored through my selenium instance?
Try loading a custom Chrome profile and see if that helps. Here's a quick example.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
See here for more details.