Selenium ChromeDriver C# - How to send a shortcut browser - c#

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???

Related

Enable Firefox Popup Blocking using C# and Selenium

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.

Accept Certificate (.p12) in C# selenium

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)

Having some trouble when opening chrome browser with Selenium ChromeDriver

I use Selenium ChromeDriver to open chrome browser and load a site into it programmatically.
I install selenium & ChromeDrive from this NuGet
Install-Package Selenium.WebDriver -Version 3.141.0 Install-Package
Selenium.WebDriver.ChromeDriver -Version 77.0.3865.4000
I have some questions:
if target pc has no chrome browser installed then how can i capture it by ChromeDriver ? is it possible?
when i am opening chrome browser by ChromeDriver instance then browser is opening chrome browser with a site but another CUI window is getting opened which i do not want to visible or i want to hide this CUI window. if it is not possible then how could i open this CUI window in minimize state?
a sample CUI window screen shot attached below when i work with FirefoxDriver. the same occur when i work with ChromeDriver instance.
when i executing this code chromeDriver.Close(); then opened chrome browser is getting closed but CUI window is still open. so if i click 5 times on open button then 5 CUI window is getting open along with 5 chrome browser instance which i had to close manually ....which i do not want to manually close it rather i want to close it when browser will be closed....how to achieve it ?
how to capture from code that opened chrome browser is close by this code chromeDriver.Close(); or if user click on cross button of chrome browser to close it?
how to open a new tab in already opened chrome browser instead of opening new chrome browser instance. if no chrome browser is open at all then new chrome browser will be open...how to achieve it by code. this below code opening new chrome browser always....what to change there for my point 5
chromeDriver = new FirefoxDriver(options);
chromeDriver.Navigate().GoToUrl("https://www.google.com");
another issue occur when i work with chrome driver that. it open chrome browser but a notification appear on browser like Chrome is being controlled by automated test software
I search google to hide it and found people said to use this option
options.setExperimentalOption("excludeSwitches", new String[] { "enable-automation" });
at my end this function does not available setExperimentalOption so what to do?
Please answer point wise with sample code.
For question 2,3 you can use below code
(use DriverService.Dispose(); to manually dispose driver service) :
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace MyProject
{
public class Browser : IDisposable
{
bool disposed = false;
IWebDriver Driver;
public Browser()
{
//Chrome Driver copied on startup path
ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(Application.StartupPath, "chromedriver.exe");
//hide driver service command prompt window
driverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
//hide browser if you need
//options.AddArgument("headless");
//or this to hiding browser
//options.AddArgument("--window-position=-32000,-32000");
//On offer Dona bhatt for disable automated test notification
options.AddExcludedArgument("enable-automation");
//options.AddArgument("disable-infobars");
Driver = new ChromeDriver(driverService, options);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
Driver.Close();
Driver.Quit();
Driver.Dispose();
DriverService.Dispose();
}
disposed = true;
}
//this method for navigation
public string Navigate(string url)
{
string page = string.Empty;
try
{
Driver.Navigate().GoToUrl(url);
page =Driver.PageSource;
}
catch
{
}
return page;
}
//this method for wait to an element be visible by element ID
private void WaitUntilLoad(string id, int timeOut)
{
WebDriverWait waitForElement = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut));
try
{
waitForElement.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));
}
catch (WebDriverTimeoutException e)
{
}
}
}
}
Use this class:
using(Browser brw=new Browser())
{
string pageSource=brw.Navigate("My URL");
}
I couldn't post this as a comment because it's too long. I think this question is off topic but I just noted my comments on your items...
This seems to be much to broad. Are you having a problem with any specific issue? All of these seems like things you can research and find out.
1) The chrome driver has to be downloaded and shipped with your app.
2) It might be possible to hide that window but not sure why that would be a hard requirement. I'm going to just say that you can't by default prevent the window from showing.
3) You're going to have to close the chrome windows through your .net code. Selenium isn't going to be graceful enough to terminate all the chrome windows.
4) I'm not sure what you're asking for. What's the problem?
5) I think tabs might be done very differently between browsers, it might not be something natively supported in selenium. There might be some chrome driver commands that can facilitate but I have no idea what they are.
6) Again, this is typically not the preferred experience, I'm guessing one of your packages has a different featureset than whatever you're reading was using.

Selenium C# Open New Tab CTRL+T Not working with CHROME

static void Main()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://google.com");
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
}
This is the code that I am trying to use to open a new tab and its not working, I am not getting any errors nothing, the driver opens Google and thats all....
I have searched a lot and found many tutorials even videos where people are using the exact same code and it works for them, but for me it doesnt and I can't figure it out...
I tried sending Keys.Shift + "t" to the search field and it works, it writes a capital T in the field
I have also tried
Actions act = new Actions(driver);
act.KeyDown(Keys.Control).SendKeys("t").Perform();
And it still does not work, but again if I change Keys.Control to Keys.Shift it writes, seems like nothing that involves Keys.Control is working!!
Edit: I have tried running the code with a IE Driver and it worked there, it opens new tab, but it does not open new tabs on Chrome?
Thanks for the answers! I did it with JavaScript.
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
Looks like it's a "feature" of the chrome driver.
https://bugs.chromium.org/p/chromedriver/issues/detail?id=581
This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked by sendKeys().
Try this
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("http://www.google.com")
If your on a mac, use Keys.Command instead of Keys.Control:
body.SendKeys(Keys.Command + "t");

Selenium-WebDriver opened the new Firefox window, but didn’t navigate to URL

My source code is copied from selenium docs site. I didn’t make any change.
http://docs.seleniumhq.org/docs/03_webdriver.jsp#user-input-filling-in-forms
I installed Selenium library via NuGet, including
Selenium Remote Control(RC), Selenium WebDriver Mono, Selenium WebDriver Support Classes, Selenium WebDriver, and Selenium WebDriver-backed Selenium.
When I run this code, a new Firefox window was opened. But the Firefox doesn’t navigate to the URL, it just stuck, nothing was loaded.
I tried the Firefox v27, v29, v30 and v31, none of them worked.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;
class GoogleSuggest
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
// Find the text input element by its name
IWebElement query = driver.FindElement(By.Name("q"));
// Enter something to search for
query.SendKeys("Cheese");
// Now submit the form. WebDriver will find the form for us from the element
query.Submit();
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
// Should see: "Cheese - Google Search"
System.Console.WriteLine("Page title is: " + driver.Title);
//Close the browser
driver.Quit();
}
}
I have had the same problem. the solution is simply uninstall your current firefox browser and download older version "i tried 2010 version 3.6.10 works great". your code is fine the problem is Mozilla people have decided to not give the right to any third party application to control Firefox.
good luck

Categories

Resources