I apologize in advance if the following question seems to be very basic, but I am very new to Selenium and I really need help.
So what I am trying to do is, I am trying to open a window popup but Chrome browser is blocking it by its own.
I am used the following code:
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("disable-popup-blocking", "true");
IWebDriver driver = new RemoteWebDriver(new Uri("http://path/to/selenium/server"), options.ToCapabilities());
But its throwing me an exception saying:
Unexpected error. System.Net.WebException: The remote name could not be resolved: 'path'.
I have tried this, But didn't help though gave me a rough idea.
Can someone please help?
(Reference: Unblocking popup using Selenium using C#)
Try as below :-
ChromeOptions options = new ChromeOptions();
chromeOptions.AddArgument("--disable-popup-blocking");
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
Edited :- if you want to use ChromeDriver instead of RemoteWebDriver try as below :
ChromeOptions options = new ChromeOptions();
chromeOptions.AddArgument("--disable-popup-blocking");
IWebDriver driver = new ChromeDriver(#"C:\path\chromedriver", options);
Hope it works...:)
Related
I am trying to hide command prompt window when starting selenium chrome, but I can't figure it out.
Here is my code:
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--headless");
var browser = new ChromeDriver(driverService, chromeOptions);
But it crash on: System.InvalidOperationException: 'session not created: This version of ChromeDriver only supports Chrome version 85
I tried to manually add path to driver but It crash's also.
Different code I used
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--headless");
chromeOptions.BinaryLocation = "C:\\Users\\TriX\\Downloads\\chromedriver_win32";
var browser = new ChromeDriver(driverService, chromeOptions)
Now it crash on: OpenQA.Selenium.WebDriverException: 'unknown error: Failed to create Chrome process.'
Thanks for help I am completely lost :(
I tried different codes as shown, but without success.
Thanks to #r000bin i managed to resolve the problem. I also find out that I installed outdated version of ChromeDriver. So, I downloaded the correct one and it started to work.
I had old ChromeDrive nuget version
Downloaded the new one and uninstalled old one
Started to work :)
I am using ChromeDriver in C# code console app, everything works well as I expect. But I need to hide the messages/logs from the chrome driver. Used below code. Still seeing the messages like:
Starting ChromeDriver 2.41.578737
(49da6702b16031c40d63e5618de03a32ff6c197e) on port 51929
Only local connections are allowed.
DevTools listening on
ws://127.0.0.1:51932/devtools/browser/09ed1c7f-c33a-4f76-990b-943c6837d8d8
ChromeOptions options = new ChromeOptions();
var chromeDriverService = ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
chromeDriverService.HideCommandPromptWindow = true;
chromeDriverService.SuppressInitialDiagnosticInformation = true;
options.AddArgument("headless");
options.AddArgument("--silent");
options.AddArgument("log-level=3");
IWebDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),options);
I did suppress the logs by adding "options.AddArgument("log-level=3");". I am trying to avoid seeing below in my console, I need to suppress these messages too:
Starting ChromeDriver 2.41.578737
(49da6702b16031c40d63e5618de03a32ff6c197e) on port 51929
Only local connections are allowed.
DevTools listening on
ws://127.0.0.1:51932/devtools/browser/09ed1c7f-c33a-4f76-990b-943c6837d8d8"
You need to pass the chromeDriverService in the ChromeDriver constructor:
IWebDriver driver = new ChromeDriver(chromeDriverService, options);
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 ?
So I am starting to learn automation using Selenium and C#, problem is i can navigate to my facebook, google what ever and all works good on firefox. But when using IE browsers it opens the webpage but then throws error 'NoSuchElementException'. I am using same code, one works one does not.
here is IE code
IWebDriver driver = new InternetExplorerDriver(#"C:\folder");
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Name("q")).SendKeys("Hello World");
Here is firefox code
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Name("q")).SendKeys("Hello World");
This could be timing issue on both browser. You should try using more stable code using WebDriverWait to wait until element is visible before interaction as below :-
IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Name("q")));
element.SendKeys("Hello World");
I have a Selenium suite which has 150 test cases. The test has to run in Incognito mode in Chrome Browser.
I am able to launch the browser in incognito Mode. But the issue is the browser is not getting maximized ( say for 10 test cases and for remaining 140 test cases the browser launches in maximized mode) , though there is a code to maximize the browser.
As a result of this, some of the test fails ( All 10 test ).
Below is my code
desiredCapabilities = DesiredCapabilities.Chrome();
var options = new ChromeOptions();
options.AddArgument(#"--incognito");
options.AddArgument("--start-maximized");
desiredCapabilities.SetCapability(ChromeOptions.Capability, options);
webDriver = new MyWebDriver(new Uri(gridHubURL), options.ToCapabilities(),TimeSpan.FromSeconds(ApplicationConfiguration.RemoteDriverTimeOutValue),testContext);
break;
Is there a way to ensure that the browser always (100%) launches in maximised mode.
The click operation fails when the browser is not maximised.
System.InvalidOperationException: unknown error: Element is not clickable at point (886, 466). Other element would receive the click:
For this reason, I want to run in maximised mode. In maximised mode, I am not getting this error. Please help .
Thanks
Try this code:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--incognito");
IWebDriver driver = new ChromeDriver("C://",options);
It works for me
Could do something like this:
desiredCapabilities = DesiredCapabilities.Chrome();
var options = new ChromeOptions();
options.AddArgument(#"--incognito");
options.AddArgument("--start-maximized");
desiredCapabilities.SetCapability(ChromeOptions.Capability, options);
webDriver = new MyWebDriver(new Uri(gridHubURL), options.ToCapabilities(),TimeSpan.FromSeconds(ApplicationConfiguration.RemoteDriverTimeOutValue),testContext);
webDriver.Manage().Window.Maximize();
break;
It will need to be after the webDriver opens up, but it will maximize the window for you.
Try this instead, I have tested and should be fine
var caps = DesiredCapabilities.Chrome();
var options = new ChromeOptions();
options.AddArgument(#"--incognito");
options.AddArgument(#"--start-maximized");
caps.SetCapability(ChromeOptions.Capability, options);
var webdriver = new ChromeDriver(options);
webdriver.Navigate().GoToUrl("http://yourURL.com");
webdriver.Manage().Window.Maximize();
An alternative would be to set the initial size:
options.AddArgument("--window-size=1024,768");
You could also set some extreme values. The window should then have the size of the screen since the OS limits it (at least on Windows) :
options.AddArgument("--window-size=32000,32000");