Start chrome and send JavaScript code - c#

When you type javascript:window.open('http://stackoverflow.com') in chrome's navigation bar, it opens new tab. I want same, but when running chrome from cmd:
var process = Process.Start("chrome.exe",
"javascript:window.open('http://stackoverflow.com')");
I'm doing this because I want to close that window later from inside it with window.close(). If I open website directly, error is thrown:
Scripts may close only the windows that were opened by it.

If you just want chrome to open a new tab to a page, why not try:
var process = Process.Start("chrome.exe", "http://stackoverflow.com");
If you just want to close the window later with JS, what about:
window.open(location, '_self', '');
window.close();
Note, you cannot automatically add the javascript prefix to a chrome URL, even with copy and paste - this is a security feature.
see - Javascript stripped from URL bar?

Related

Not able to get windowhandle of already open IE window

I am trying to automate citrix login process at my organization.
I am using ChromeDriver ( selenium ) to automate few steps which is successful till now.
But it downloads .ica file which I need to double click and which opens fresh IE browser instance.
I am not able to get handle of this instance. I want to enter username and password programmatically in this newly opened IE window.
My code is as below.
// Code which downloads ica file
var idCitrix = chromedriver.FindElementById("IdOfDOMElement");
idCitrix.Click();
// Below code will ALWAYS OPEN IE FRESH BROWSER INSTANCE (IE 11)
Process.Start("LOCATION OF .ICA FILR FROM DOWNLOADS FOLDER");
//How to get handle of this newly opened IE window ??

In C# open the web browser and capture the output

From C# console application I need to open the web browser and i want the output. This is the server name in the link. If internet explorer displays nothing then it means server is up. If I get internet explorer cannot display the webpage then it means server is down.
Below is the code to open the IE
Process.Start("https://foo.com");
If internet explorer shows an empty page then server is up else server down.
And want to know how the browser closes automatically?
using(WebClient client = new WebClient())
{
string pageData;
try
{
pageData = client.DownloadString(yourAddress);
}
catch(Exception e)
{
//something went wrong. Maybe the site is down?
}
//does pageData have expected content?
}
If you just need to get the value of a web page, you should use something more lightweight, for example, HttpClient (http://pfelix.wordpress.com/2012/01/11/the-new-net-httpclient-class/). This way you won't have to worry about cleaning up external processes like internet explorer.
If you insist on using IE for this, Process.Start(string) returns an instance of System.Diagnostics.Process, so you can call .Close() on that returned instance.
.
var ieProcess = Process.Start("https://foo.com");
bool isServerUp = IsServerUp(ieProcess);
ieProcess.Close();

http authorization with webdriver and popup window

I'm trying to use Selenium WebDriver to automatically login in to a site with a user-name and password. I've done my research and I don't believe this feature is supported by WebDriver, so I need to find another way. The site I'm trying to automate logging into is located here.
When prompted to login a popup window comes up that doesn't seem to be part of the browser. I'm using Firefox and Chrome. It seems Windows API may be required? I already tried passing the credentials in the URL but that didn't work. Also tried sendkeys, but received a Windows exception that the application was not accepting Windows messages. I also tried switching the current handle using driver.windowhandles but the popup doesn't seem to be a new handle.
Does anybody have any ideas? I'm kinda stuck. The preliminary code to get to the popup window is:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.portal.adp.com");
string currentWindow = driver.CurrentWindowHandle;
IWebElement userLogin = driver.FindElement(By.Id("employee"));
userLogin.Click();
The popup you are seeing is prompted by web server and is a authentication prompt. Selenium doesn't support this operation.
One of the way to handle this limitation is to pass user and password in the url like like below:
http://user:password#example.com
More info available here : http://aleetesting.blogspot.in/2011/10/selenium-webdriver-tips.html
I wanted my answer out there because I think I've solved it. This answer does not require passing the credentials through the URL (for those of you that are unable to like me). It also does not require any custom Firefox Profiles or extensions to be installed or included with the solution or installed onto the browser eliminating cross-machine compatibility issues.
The issue with me was that the authentication could not be completed via passing the credentials through the URL because the login was behind a proxy.
So, I turned to windows automation toolkits and found AutoIT. Using AutoIT and Selenium, you can login automatically by sending the username and password to the windows dialog that appears. Here's how (note the steps below are for c#:
1 - Download AutoIT from http://www.autoitscript.com/site/autoit/downloads/
2 - Add the autoit .dll to your project references.
Right click on references, select Add Reference. Next click the browse button and browse to the dll location (most default installations it will be c:\Program Files (x86)\AutoIt3\AutoItX\AutoItX3.dll), and add to project.
3 - use AutoIT and Selenium like this (assuming your web driver is already initialized):
//Initialize AutoIT
var AutoIT = new AutoItX3();
//Set Selenium page load timeout to 2 seconds so it doesn't wait forever
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(2));
//Ingore the error
try
{
Driver.Url = url;
}
catch
{
return;
}
//Wait for the authentication window to appear, then send username and password
AutoIT.WinWait("Authentication Required");
AutoIT.WinActivate("Authentication Required");
AutoIT.Send("username");
AutoIT.Send("{TAB}");
AutoIt.Send("password");
AutoIT.Send("{ENTER}");
//Return Selenium page timeout to infinity again
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(-1));
Anyway, that's it, and it works like a charm :)
Also note that there are some special characters that need to be escaped in AutoIT using the sequence "{x}". For example, if your password is "!tRocks", you'd need to pass it into AutoIT as "{!}tRocks".
Happy automating.
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.http.phishy-userpass-length", 255);
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", hostname);
Driver = new FirefoxDriver(profile);
hostname is your URL (example.com) then try to
Driver.Navigate().GoToUrl(http://user:password#example.com);
I just got done working on a prototype project that is supposed to handle exactly this kind of situation.
It utilizes BrowserMob, a popular open source proxy, to perform the authentication.
SeleniumBasicAuthWrapper Hope it helps! It is still a work in progress, but hopefully we'll get any kinks or defects ironed out in the near future.

Starting internet explorer in open tab

If System.Diagnostics.Process.Start("IEXPLORE.EXE", url); opens a new browser session, how do you open a new browser window in an existing version of internet explorer?
Thanks
Try Process.start(url) it will open up a new Tab if browser is already running, otherwise it will open default browser and shows the Page.
Check this out
Activate existing browser window with given URL from C# application (without triggering reload)
Hope that helps

Open URI in new TAB (Silverlight)

Hey SO, is there anyway I can use HtmlPage.Window.Navigate(new Uri(link), "_blank"); to open a URI in a new tab (not new window!) which is in the same instance of Internet Explorer.
Currently using SL3 and it seems that whether its a new tab vs. new window is based on browser options...
Any help? Thanks.
You're doing it properly by using "_blank". What that does is opens a new page in either a new tab or window depending on the user's settings:
Internet Explorer Settings
Firefox
Safari
If you want to use a "hack" to get around this use this link as a resource. It basically states that until a browser supports CSS3 you'll have to use a "hack" as a workaround:
Internet Explorer -- IWebBrowser2::Navigate2 (use 0x1000 as the second parameter). Note: you have to have FULL TRUST MODE for this to work.

Categories