how to press Ctrl + ] in Selenium web driver - c#

I have the ff.code which opens a chrome browser and press "F12" to open chrome dev tools, I've been searching in google on how to press Ctrl+] but unfortunately did not find any article/info about it. Has anyone here made this stuff? thanks!
IWebDriver wdriver = new ChromeDriver();
wdriver.Navigate().GoToUrl("www.samplewebapp.com");
wdriver.Manage().Window.Maximize();
Actions action = new Actions(wdriver);
action.SendKeys(OpenQA.Selenium.Keys.F12).Perform();

Use either
Actions action=new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "]").Build().Perform();
or
yourWebElement.SendKeys(Keys.Control + "]");

Related

How to set the download folder for Edge in Selenium?

I am writing automated tests using Selenium. I want to set the download directory in Edge so that I can download files as part of my test. There is an EdgeOptions object that I can provide when creating the EdgeDriver, but I don't know what to set on the EdgeOptions.
I know the equivalent of how to do this in Chrome
chromeOptions.AddUserProfilePreference("download.default_directory", #"C:\temp")
and Firefox
firefoxOptions.SetPreference("browser.download.dir", #"C:\temp")
But, how do I do the same thing in Edge? And get it to download automatically without a save prompt?
As #Prany already mentioned, probably there is no way to set download automatically. And if I right understood, you want to handle with native window dialogue, when you are clicking on download button. Selenium cannot interact with native windows, but you can use this framework. The sample code would be like this:
// Press the A Key Down
KeyboardSimulator.KeyDown(Keys.A);
// Let the A Key back up
KeyboardSimulator.KeyUp(Keys.A);
// Press A down, and let up (same as two above)
KeyboardSimulator.KeyPress(Keys.A);
// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);
// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);
So you can simulate Ctrl + V keyboard action and Enter action. Hope it helps.
You can do this for Edge like this:
// hide driver Console? true/false
EdgeDriverService service = EdgeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true; // hide Console
// change Standard-Download-Path
EdgeOptions options = new EdgeOptions();
var downloadDirectory = "C:\temp";
// Setting custom download directory
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
// start Selenium Driver:
webdriver = new EdgeDriver(service, options);
// max. Window
webdriver.Manage().Window.Maximize();

actions.MoveToElement() not working on ie 11

The same method used in the test automation project I wrote in c # does not work in internet explorer 11 even though the movement method I use is chrome, firefox and edge. It does not give any errors, but the next action is fail
log.Debug("fare " + by + " üzeriine dogru haraket ediyor, webelement label ");
IWebElement element = GetElement(by);
Actions Actions = new Actions(Driver);
WaitElementToClickable(Driver, by, 5);
Actions.MoveToElement(element);
Actions.Perform();
WaitElementToClickable(Driver, by, 5);
I spent a long time trying to get actions to work across all browsers, and for IE I found the following helped.
Selenium webdriver v2.29.0 (https://github.com/SeleniumHQ/selenium/blob/master/java/CHANGELOG) added:
IEDriver supports "requireWindowFocus" desired capability. When
using this and native events, the IE driver will demand focus and
user interactions will use SendInput() for simulating user
interactions. Note that this will mean you MUST NOT use the
machine running IE for anything else as the tests are running.
When I set the IEDriver I use:
InternetExplorerOptions options = new InternetExplorerOptions();
options.requireWindowFocus();
webDriver = new InternetExplorerDriver(options);
And all my move to and click events work fine. I'm using IE11.125-11.309 and Selenium (java bindings) 3.7.1.

Open a new tab in an existing browser session using Selenium

My current code below in C# opens a window then navigates to the specified URL after a button click.
protected void onboardButton_Click(object sender, EventArgs e)
{
IWebDriver driver = new ChromeDriver();
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.Navigate().GoToUrl("http://www.google.com")
}
But the site that I am planning to navigate to has single sign-on. How can I open a new tab in my existing browser session and navigate from there? The above code does not seem to work.
Sending Keys.Control + "t" didn't work for me. I had to do it with javascript and then switch to it.
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
To handle new tab you should switch to it first. Try following:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("http://www.google.com")
Also you might need to switch back:
driver.SwitchTo().Window(driver.WindowHandles.First());
This may not works:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
Alternative: Find clickable element with target blank (search for "blank" in page's surce code). This will open new tab.
Than switch between tabs (thanks #Andersson) with:
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.SwitchTo().Window(driver.WindowHandles.First());
We can simulate Ctrl + Element Click
Actions action = new Actions(_driver);
action.KeyDown(Keys.Control).MoveToElement(body).Click().Perform();
Reference:
https://www.codeproject.com/Answers/1081051/Open-link-in-New-tab-using-Selenium-Csharp-for-chr#answer3
The solution is really simple:
Driver.SwitchTo().NewWindow(WindowType.Tab);
Enjoy...
IWebDriver driver = new ChromeDriver();
Change this to:
var driver = new ChromeDriver();
I do not know why. May be the IWebDriver miss the method.

Open link in new tab selenium c#

I am writing a program to run videos listed on my site for testing purpose and here what I need is to run videos in different tabs of the same browser window.
I have hundred video urls in the List videoLinks = getVideoUrls();
and now what I need is to execute these videos 5 at a time.
ChromeDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.withoutabox.com" + videoLink);
If I go the above way then for all videos I will have to create a new ChromeDriver object. I want to use single chrome browser object.
I have tried this
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
it only adds a new tab but not open a link there.
Please let me know how should I go around it. I have googled but couldn't find my solution so thought to ask for help.
Try this:
public void SwitchToTab(object pageId)
{
webDriver.SwitchTo().Window(pageId.ToString());
}
You can use CurrentWindowHandle to find current tab.
webDriver.CurrentWindowHandle;
For your scenario I'm using that code:
public IPageAdapter OpenNewTab(string url)
{
var windowHandles = webDriver.WindowHandles;
scriptExecutor.ExecuteScript(string.Format("window.open('{0}', '_blank');", url));
var newWindowHandles = webDriver.WindowHandles;
var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
webDriver.SwitchTo().Window(openedWindowHandle);
return new SeleniumPage(webDriver);
}
Update
Window open create new popup. By default this option can be blocked by browser settings. Disable popup blocking in your browser manually.
To check this, open js console in your browser and try to execute command window.open('http://facebook.com', '_blank');
If new window open successfully than everythng is OK.
You can also create your chrome driver with specific setting. Here is my code:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("profile.default_content_settings.popups", 0);
return new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromSeconds(150));
Here is a simple solution for open a new tab in seleneium c#:
driver.Url = "http://www.gmail.net";
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");

C#: How to send key combination in selenium's Keyboard.SendKeys

I'm using Selenium 2 on Windows to automate firefox. I tried to send 'Alt+Esc' to the browser to minimise it on start. However firefox keeps typing the '{%ESC}' in its address bar. What should I do? thanks.
using (driver = new FirefoxDriver(firefoxProfile))
{
driver.Keyboard.SendKeys("{%ESC}");
}
I use this to open a new window (with the keyboard input) and it works just fine
IWebDriver driver = new FirefoxDriver();
Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "n").Build().Perform();
I prefer:
element.SendKeys(Keys.Control + "a");
Now you can focus on the element and not only on the webdriver
Sending regular key use SendKeys("ab12#$").
Sending a modifier key (Keys.Shift, Keys.Control, or Keys.Alt) use
.[KeyUp\keyDown]([OpenQA.Selenium.Keys.Control\OpenQA.Selenium.Keys.Alt...])
And you can combine them as needed:
action.KeyDown(OpenQA.Selenium.Keys.Shift).SendKeys("ThisWillBePrintedCAPS").KeyUp(OpenQA.Selenium.Keys.Shift).SendKeys("ThisWillBePrintedRegular").Build().Perform();

Categories