Hi.
How can I click on 'Allow' button? I really can't find any solution in the internet. I don't want to use c++ hooks or simulate mouse clicks by coordinates. Is there any good way to allow this notification?
new ChromeOptions.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 2);
doesn't help
Found two solutions:
1) Thanks for #Floren 's answer: C# selenium chromedriver click on Allow store files on this device
There is an argument for Chromium --unlimited-storage
Chromium source code reference:
// Overrides per-origin quota settings to unlimited storage for any
// apps/origins. This should be used only for testing purpose.
const char kUnlimitedStorage[] = "unlimited-storage";
# Prevent the infobar that shows up when requesting filesystem quota.
'--unlimited-storage',
C# usage:
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--unlimited-storage");
var driver = new ChromeDriver(chromeOptions);
2) #Simon Mourier's answer C# selenium chromedriver click on Allow store files on this device
Click on Allow button using .NET UIAutomation
var andCondition = new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button), new PropertyCondition(AutomationElement.NameProperty, "Allow"));
AutomationElement chromeWindow = AutomationElement.FromHandle(_windowPointer); // IntPtr type
var buttonsFound = chromeWindow.FindAll(TreeScope.Descendants, andCondition);
if (buttonsFound.Count > 0)
{
var button = buttonsFound[0];
var clickPattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
clickPattern.Invoke();
}
You can't, this is an OS level dialogue, not something inside the DOM.
The way to get around it is by using desired capabilities to configure chrome to not show this dialogue.
I'm going to suggest
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("download.prompt_for_download", 0);
options.AddUserProfilePreference("settings.labs.advanced_filesystem", 1);
Other potential commands to add the options if AddUserProfilePreference doesn't work would be:
AddLocalStatePreference
AddAdditionalChromeOption
AddAdditionalCapability
For more details about desired capabilities and chrome have a look at:
The ChromeOptions documentation
This list of command line switches, or the command line switched defined directly in code.
The preferences defined directly in code
The ChromeOptions class in the Selenium codebase
var chromeOptions = new ChromeOptions();
var downloadDirectory = #"C:\Users\";
chromeOptions.AddUserProfilePreference("download.default_directory", downloadDirectory);
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
chromeOptions.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
IWebDriver _driver = new ChromeDriver(chromeOptions);
Can you try with this one ?
Related
We use ChromeDriver in C# to connect to existing instances of Chrome that have the remote debugging port 9222 set. Here is how we connect:
var svc = ChromeDriverService.CreateDefaultService(path);
ChromeOptions options = new ChromeOptions();
options.DebuggerAddress = "127.0.0.1:9222";
var driver = new ChromeDriver(svc, options);
var url = driver.Url;
The problem is that the value of driver.Url is not what it used to be when using ChromeDriver version 88.
At that point and all earlier versions, driver.Url was the value of the URL for the current active tab in Chrome. So if Chrome had five tabs open and tab 4 is active, the Url was that of tab 4. And that made sense.
Once we upgraded to version 90 that is no longer the case. It appears that the value of Url is... well it's not clear. Sometimes the last active tab, sometimes some other tab, sometimes the first. I do not see a pattern.
Is this an error in ChromeDriver? In the past, whatever was the active tab was the one that driver.Url yielded. Now it's indeterminate which wreaks havoc with our code.
Update: If I have two tabs open, then the driver.Url and driver.Title are for the tab that was just prior active. So always the other tab. With 3 tabs it may be the 2nd to the last active tab. This feels like a off-by-one error within an internal array of tabs.
I had the same problem. I solved it using the method suggested here
ChromeDriverService driverService =
ChromeDriverService.CreateDefaultService();
var options = new ChromeOptions();
options.DebuggerAddress = "127.0.0.1:9222";
var driver = new ChromeDriver(driverService, options);
driver.SwitchTo().Window(driver.WindowHandles[0]); // Switch to the recently opened tab
MessageBox.Show("driver.Url: " + driver.Url);
I am trying to find a way how to accept a certificate popup window in C# selenium.
I tried several ways.. nothing helped.
new Thread(() =>
{
driver.Navigate().GoToUrl(url);
}).Start();
driver.SwitchTo().Alert().Accept(); // here I tried many type of ways to accept the popup, also key:enter ...
also I tried some ignore stuff to add in
chromeOptions.AddArguments(); but it didn't help neither.
several ideas are not actual - for example DesiredCapabilities is not available anymore.
didn't help:
options.AddArgument("ignore-certificate-errors");
chromeOptions.AddAdditionalCapability(CapabilityType.AcceptSslCertificates, true);
thank you for any idea or support
The following code works (with "--" at the beginning of the option name):
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--ignore-certificate-errors");
var chromeDriverLocation = Environment.GetEnvironmentVariable("ChromeWebDriver");
if (string.IsNullOrEmpty(chromeDriverLocation))
{
chromeDriverLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
}
WebDriver = new ChromeDriver(chromeDriverLocation, chromeOptions);
(Full code is here)
I am attempting to control two browser windows via selenium using c# and a single chromedriver. The reason being that I need to share session details accross browser windows.
The code that I have tried and failed with is below;
var options = new ChromeOptions();
options.AddArguments("chrome.switches", "--disable-extensions --disable-extensions-file-access-check --disable-extensions-http-throttling --disable-infobars --enable-automation ");
options.AddUserProfilePreference("credentials_enable_service", false);
options.AddUserProfilePreference("profile.password_manager_enabled", false);
options.PageLoadStrategy = PageLoadStrategy.Default;
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
var Driver = new ChromeDriver(service, options);
//THIS WILL OPEN A NEW WINDOW. BUT BECAUSE IT IS A NEW DRIVER DOES NOT WORK FOR SHARING SESSION DETAILS.
//var TestDriver = new ChromeDriver(service, options);
//TestDriver.Manage().Window.Maximize();
//THIS JUST OPENS UP A NEW TAB. NOT A NEW WINDOW (IT WOULD SEEM MOST DOCUMENTATION SUGGESTS THAT IT SHOULD)
IJavaScriptExecutor jscript = Driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open();", "google.com.au");
//TRY USING THE SEND KEYS TECHNIQUE. NOTHING HAPPENS
var test = Driver.FindElement(By.TagName("html"));
test.SendKeys(Keys.Control + "n");
test.SendKeys(Keys.Control + "t");
//TRY AGAIN USING THE SEND KEYS TECHNIQUE USING A DIFFERENT TAG. NOTHING HAPPENS
var blah = Driver.FindElements(By.TagName("body"));
blah[0].SendKeys(Keys.Control + "t");
//TRY USING ACTIONS. NOTHING HAPPENS
Actions action = new Actions(Driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "n");
action.Build().Perform();
I may resort to AutoIt to open a browser if I have to, but one more dependency is not what I need. Documentation everywhere around the web seems to suggest than all the options I tried above should work...I suspect it may be a chromedriver issue of some kind.
Any ideas on how to achieve my goal would be greatly appreciated
UPDATE.
Arnons answer below lead me to the solution. If you are in a similar situation the best thing to do is just open up the browser console (from developers tools) and experiment with javascript until you get what you want. Then just execute that. In the end executing the following code has worked for me.
IJavaScriptExecutor jscript = Driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open('https://www.bing.com.au','_blank','toolbar = 0, location = 0, menubar = 0')");
The other alternative was to use Autoit, which I also got working, much easier than I did figuring out the javascript. But one less dependency is best :)
UPDATE2.
Further complications arise with trying to control the window as an independent browser window. I believe any new window created from a parent window, has the same process id (at least my testing has indicated so), and for all intense and purpose is treated as a tab in the selinium driver. I therefore conclude that certain things are just not possible (for example relocating the child browser window on the screen).
Your first attempt using ExecuteJavaScript was very close, but In order for it to open a new window instead of new tab, you should add the following arguments: `"_blank", "toolbar=0,location=0,menubar=0" to it.
See this question for more details.
I should have read the question better, here is my solution. Ended up using this for selecting windows that popped up after clicking a button but should work with swapping between windows.
//---- Setup Handles ----
//Create a Handle to come back to window 1
string currentHandle = driver.CurrentWindowHandle;
//Creates a target handle for window 2
string popupWindowHandle = wait.Until<string>((d) =>
{
string foundHandle = null;
// Subtract out the list of known handles. In the case of a single
// popup, the newHandles list will only have one value.
List<string> newHandles = driver.WindowHandles.Except(originalHandles).ToList();
if (newHandles.Count > 0)
{
foundHandle = newHandles[0];
}
return foundHandle;
});
//Now you can use these next 2 lines to continuously swap
//Swaps to window 2
driver.SwitchTo().Window(popupWindowHandle);
// Do stuff here in second window
//Swap back to window 1
driver.SwitchTo().Window(currentHandle);
// Do stuff here in first window
You need to explicitly tell Selenium which tab you wish to interact with, which in this case would be;
driver.SwitchTo().Window(driver.WindowHandles.Last());
I'm using Selenium webdriver 3.3 and ChromeDriver 2.28 (32bit). My slave machine is Windows 7 running Chrome 57.
When Chrome starts I am trying to use ChromeOptions over RemoteWebDriver to disable the "save your password" pop-up using the parameter "password_manager_enabled".
However, it seems to have no effect at all. I have tried many variations around ChromeOptions, JSON strings and simple strings but all to no avail.
ChromeOptions cOpt = new ChromeOptions();
cOpt.addUserProfilePreference("profile.password_manager_enabled", false);
var capabilities = chromeOpts.ToCapabilities() as DesiredCapabilities;
// Add OS, Platform capabilities etc
string gridConnectionURL = "xxxx"
driver = new CustomRemoteDriver(new Uri(gridConnectionURL), capabilities, new TimeSpan(0, 5, 0));
Does anyone know the "correct" way to set this preference so it works?
You just need to replace
//cOpt.AddUserProfilePreference("password_manager_enabled", "false");
cOpt.AddUserProfilePreference("credentials_enable_service", false);
cOpt.AddUserProfilePreference("profile.password_manager_enabled", false);
I am using selenium to automate website filling in chrome. When I download exe or XML files I am getting a popup 'This type of file can harm your computer' with keep and discard options. How to disable this programmatically ?
I am implementing this in c#,
I have tried,
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true")
But this is not working for me.
How to disable this popup ?
If not possible how to accept the warning ?
Please help me.
To disable the message, you need to set the preference safebrowsing.enabled to true. Here is a working example with CSharp:
var options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", "C:\\Downloads");
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("download.directory_upgrade", true);
options.AddUserProfilePreference("safebrowsing.enabled", true);
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://www.7-zip.org/a/7z1602.exe");
And for a description of the preferences:
https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc
Probably what you need is change
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true") // disables blocking the popup
to
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "false") // enables blocking the popup
instead to avoid the popup window.
Edit : In case you are getting stuck with the harmful file contents, you can try and set an experimental chrome options using :
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("safebrowsing.enabled", "true");
chromeOptions.setExperimentalOption("prefs", prefs);
Quoting a sample from the doc here :
Set a Chrome preference
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);
Date - 4 June,2016 [C#]
Went through few links and could figure this out that .Net still doesn't have an setExperimentalOption preferences along with the ChromeOptions. So, one of the way to Add Argument to the ChromeOptions using C# with the flags listed here would be :
chromeOptions.AddArgument("--safebrowsing-disable-download-protection");
Disables safebrowsing feature that checks download url and downloads
content's hash to make sure the content are not malicious.
Date - 4 June,2016 [JAVA]
The document here quotes it as :
public void setExperimentalOption(java.lang.String name,
java.lang.Object value)
Sets an experimental option. Useful for new ChromeDriver options not yet
exposed through the ChromeOptions API.