Navigate driver to new opened window in selenium with C# - c#

I wrote a simple code to submit a sign up form with Selenium. Before submit, driver should come from home page to sign up page.
var firefox = new FirefoxDriver();
firefox.Navigate().GoToUrl("http://mywebsite/home");
If I print firefox.Title, it shows me title of home page currectly
And in home page, there is a sign-up button. Sign up button link is bellow.
<a target="_blank" href="SignUp.jsp">Register Here</a>
To navigate to sign up page, I wrote a line:
firefox.FindElement(By.CssSelector("a[href='SignUp.jsp']")).Click();
After this, driver shows me the sign up page in new window of firefox browser. To navigate driver to the sign up I wrote firefox.Navigate();
Now If I print firefox.Title, it shows me title of home page again.
Please help me to find out problem. Thanks in advance.

You pretty much grabbing the same title since the you never switched to newly opened window
// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;
// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[#id='webtraffic_popup_start_button']"));
// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);
driver.SwitchTo().Window(popupWindowHandle);
// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchToWindow(currentHandle);
And, after switching to new window you should get new title.
However, this window handles process is utterly confusing to me. Selenium .Net bindings provide PopupWindowFinder class to handle windows.
Gratitude to JimEvans for his nice works and this

Use
firefox.SwitchTo().Window(handle);
where handle is one of instances found in firefox.WindowHandles. This will switch between the different window instances. You can find more information in the docs for IWebDriver.SwitchTo().

Related

Automating 'To' from Contacts List Gmail

When you compose an e-mail in Gmail, to send the email to people on your Contacts list you have to click 'To' which leads you to a pop-up box. Is there a way to automate the closing of this box? I'm using Selenium and C#.
I think you're likely running into problems because that modular window is within an iFrame, and you need to tell Selenium to specifically look into that iFrame before searching.
See handling-iframes-using-selenium-webdriver.
Unfortunately, Google seems to assign a new ID to this iframe every time it's created, so you can't rely on that, so you'll need to create an IWebElement for the iframe element and pass it into the .SwithTo().Frame() call.
Specifically, you could try this (I tested this out, and it worked for me).
//Get iFrame element and switch to it.
IWebElement selectContactFrame= driver.FindElement(By.CssSelector("iframe.KA-JQ"));
driver.SwitchTo().Frame(selectContactFrame);
//Find cancel button and click it.
IWebElement cancelButton = driver.FindElement(By.XPath("//div[text()='Cancel']"));
cancelButton.Click();

How to verify click to call browser functionality with selenium webdriver

I have a webpage where it contains 1300 number and i just need to verify that once i click on that link then it will open browser Popup window and i can capture the details from that popup and verify test is passed.
I need to do it for Website . Please help me with how to verify that popup.
Follow these steps:
1.Store window handle id of first window.
2.Store collecion of link elements
3.Iterate the collection
3.1 Click the element
3.2 Find the new window
3.3 Switch to the new window
3.4 Assert content
3.5 Switch back to the window handle id of first window (which is store at #1)
See example here
Its very simple
Create driver object
var IEBrowser = new InternetExplorerDriver();
Open URL
ActionHelper.Timeout_Code(() => IEBrowser.Navigate().GoToUrl(URLTOWEBSITE), 15000, () => IEBrowser.Navigate().GoToUrl(URLTOWEBSITE));
Verify Popup page by selecting any control from popup.

Selenium webdriver in c#-Unable click on hyperlink on frame

I am doing automation using selenium Webdriver in c#.So in my application, after clicking on one link, there is frame opened on top of main browser window
So i am able to read text on that frame, in short i can switch to that frame.
Frame having many hyperlink, and i have to click on hyperlink named "Add/Change Admin Message". But i uses below code to click on link but it is scrolling down the frame in IE browser so click action is not performed.But its works fine in chrome browser.
Driver.FindElement(By.XPath("//a[contains(text(),'"+"Add/Change Admin Message"+"')]")).Click();
I have tried with xpath,cssselector,classname,Id,linktext,partial link text but no luck.
Below is the DOM for that link, please help me ..
Add/Change Admin Message
First, you need to switch to the frame where the link is:
Driver.SwitchTo().Frame("frame name or id");
Then, you can locate the link by partial link text:
Driver.FindElement(By.PartialLinkText("Add/Change Admin Message"));
With IE it is often something magical you should do, try clicking the link via javascript:
IWebElement link = Driver.FindElement(By.PartialLinkText("Add/Change Admin Message"));
IJavaScriptExecutor js = Driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", link);

Save current window URL and move back to that URL after couple of navigation

I have a site and in my site when user click on a specific div then a new child window open . Suppose the user is standing at xyz.com/category , now if you user clicks on that div then parent windows goes to another url suppose google.com and a popup window will get open and there are some more navigation in popup windows . But every page that will open in popup has a button called Go Back To Site . I want that when user clicks on several links in popup and then click on Go Back To Site button then popup window get closed and parent window move back to xyz.com/category.
You can try PHP Superglobal array
$_SERVER['HTTP_REFERER']
If you can use javascript for this, there are simple functions such as:
window.history.back();
window.history.forward();
You can refer all of them in following link:
https://developer.mozilla.org/en/docs/DOM/Manipulating_the_browser_history
For Jquery:
$(document).ready(function() {
var referrer = document.referrer;
});
For C# :
WebBrowser1.Url = new Uri("http://maps.google.com");
- document.referrer does not work for IE
For this issue refer following code:
<script type="text/javascript" >
function redirect(url) {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
var referLink = document.createElement('a');
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
} else {
location.href = url;
}
}
</script>
This code can also be refered over here
The way I'd do it is to pass the current URL as a parameter into your popup. So you would change your anchor link tags to this:
Click
So when your popup pops up, you just grab the value of prevPage and then when they click "Back to previous page", you just redirect to the value of prevPage.

WatiN: MsHtmlBrowser will not TypeText

From the WatiN website:
// Open a new Internet Explorer window and
// goto the google website.
IE ie = new IE("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
The above sample works just fine. However, since I do not want to open up a browser window, I modified the above code to use MsHtmlBrowser:
// goto the google website.
var ie = new MsHtmlBrowser();
ie.GoTo("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
The TypeText line is throwing an exception. Any idea what's wrong?
The MsHtmlBrowser is only there to find elements and read their attribute values. There is no support for clicking a link, typing text, firing events, no session state or any other way of interact like with a normal browser. So us it for scrapping only.
HTH,
Jeroen

Categories

Resources