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";
Related
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.
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
I am using Selenium (2.24) to generate unit tests (for the Visual Studio unit test framework). While using the C# WebDriver for FireFox, it appears that the browser which is fired up by the driver is not finding my website cookies via javascript (I have a javascript file included in the site that looks for cookies and lets me know if they are found). Also, it is not using the browsers image cache, and is always requesting new images from the server. This behavior does not happen when I run my site from the "normal" (not launched by Selenium) FireFox.
The strange thing is that calling the below code in my unit test DOES return my cookie (it just can't be found by my JavaScript)
driver.Manage().Cookies.GetCookieNamed("MyCookie");
How can I configure the driver to respect my cookies and use the browsers image cache? This functionality is key to testing my website.
By default the FirefoxDriver will create a new anonymous Profile each time it starts Firefox. If you want it to use an exiting profile you need to tell it to.
In Java you do it like so:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("MyProfile");
WebDriver driver = new FirefoxDriver(profile);
I'm assuming there's something similar in C#
For cookies: if cookie is marked as "HTTP Only" JavaScript on a page will not be able to see it. As result any code that uses execution of JavaScript on the page will not see this particular cookie.
You can confirm it by using some HTTP debugger (i.e. Fiddler) to see if cookie is set with HttpOnly property. You also can check if running script on a page via dev tools or typing javascript:alert(...) in address bar can see the cookie (document.cookie)