I'm using the Selenium 3.14 Webdriver to test a site with Firefox. (I test other browsers also - but this problem is with Firefox).
For some tests I want to enable popup blocking. My Firefox instantiation is:
driver = new FirefoxDriver(geckodriverDirectory);
driver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(1.0d);
I tried adding an option, as follows:
var options = new FirefoxOptions();
options.SetPreference("disable-popup-blocking", false);
driver = new FirefoxDriver(driverDir, options);
driver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(1.0d);
But that failed to enable popup blocking. Any ideas for setting the correct Firefox option?
I finally solved this by setting the following preference instead of the preference shown above:
options.SetPreference("dom.disable_open_during_load", true);
The true value seems counter intuitive - because the impact is to have popup blocking enabled - but it works.
It would be great if Firefox would publish a list of preferences that their webdriver recognizes.
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 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());
How can I send a Chrome shortcut with Selenium ?
I mean shortcuts like Ctrl+S, Ctrl+T or Ctrl+P which has nothing to do with WebElements. I read a lot of similar questions there, but none of the suggested solutions work for me.
Let's say I want to open a new tab (Ctrl+T) on the browser, I tried all the following code without success:
The "standard" way :
IWebElement body = myDriver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
The action way :
Actions action = new Actions(myDriver);
action.SendKeys(Keys.Control + "t").Build().Perform();
The ChromeDriver way 1 :
if(myDriver is ChromeDriver)
{
ChromeDriver chromeDriver = myDriver as ChromeDriver;
chromeDriver.Keyboard.SendKeys(Keys.Control + "t");
}
The ChromeDriver way 2 :
ChromeDriver chromeDriver = myDriver as ChromeDriver;
chromeDriver.Keyboard.PressKey(Keys.Control);
chromeDriver.Keyboard.PressKey("t");
chromeDriver.Keyboard.ReleaseKey(Keys.Control);
chromeDriver.Keyboard.ReleaseKey("t");
Notice that the first way i mentionned worked for me with other WebDriver than Chrome.
I use :
Selenium 3.0.1
ChromeDriver 2.27.440174
And my driver's initialization is really basic :
ChromeOptions options = new ChromeOptions();
this.myDriver = new ChromeDriver(/* my path */, options);
Any ideas?
It seem to be Chromium issue. You cannot use keys combinations with chromedriver, but you still can use JavaScript as alternative:
IJavaScriptExecutor js = myDriver as IJavaScriptExecutor;
js.ExecuteScript("window.open()"); // Open new browser tab like `CTRL + t` do
Unfortunately this issue currently prevents chrome from reacting to shortcuts like Ctrl+T sent by selenium.
I'm using key combinations with Actions just fine. I have been using this code example for years and it works with Chrome, Firefox, and IE.
public void SelectAll()
{
(new Actions(yourDriverInstance)).SendKeys(Keys.Control).SendKeys("a").Perform();
}
Am I missing something???
My original question was unclear I was really looking for a way to start IE with a clean session running on the Grid. I thought the Selenium solution to this was broken, turns out it was how I was using it :facepalm: So I have updated my question to reflect that part.
So my issue is that I cannot get IE to start in a clean session when I run it on Selenium Grid. I have done a fair share of research and implemented the DesiredCapabilities that is supposed to handle this for IE.
internetExplorerCapabilities.SetCapability(ieOptions.EnsureCleanSession.ToString(), true);
But sadly this is not working and I have opened a ticket with the selenium developers if you are interested in tracking it.
Your code ieOptions.EnsureCleanSession.ToString() will return true or false and not ensureCleanSession. To set the capability:
var options = new InternetExplorerOptions();
options.EnsureCleanSession = true;
options.RequireWindowFocus = true;
var capabilities = (DesiredCapabilities)options.ToCapabilities();
capabilities.SetCapability(CapabilityType.Version, "11");
var driver = new RemoteWebDriver(new Uri("http://10.34.161.112:5555/wd/hub"), capabilities);
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");