Switch to other site from main application - selenium-webdriver

In my current application, manually when i click on a Button say 'Buy' button it takes me to a different site within the same browser (in another tab).
Usually I can switch to the Tab using
driver.switchTo().defaultContent();
But while am doing it in automation the second site is opening in a different browser. How can i handle this. I want this to open within a the same browser like how its happening when i do it manually. Please help me out. Thanks in advance.

You don't define the programming language, but in Java it is as follows:
// Store the current window handle
String winHandleBefore = driver.getWindowHandle();
// Perform the click operation that opens new window
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
// Close the new window, if that window no more required
driver.close();
// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
// Continue with original browser (first window)
Source: How to switch to the new browser window, which opens after click on the button?

Related

How can I click on a popup window in selenium Java

I am new to selenium and try to automate (using selenium Webdriver in Java for Window and using Chrome Driver) an App (ABC Project) which contains a Registration Form.
After completing the form and click on Registration Button I get a popup info message (modal) with close (X) & OK Button & Headline in the message: Under ABC Project the following is displayed & Text which informs the user that the registration was successful.
I have tried several ways to click on OK button in this popup window but no success:
1.
Alert alert = driver.switchTo().alert();
alert.accept();
2.
driver.findElement(By.xpath("//input[#value = 'alert']")).click();
Alert javascriptAlert = myTestDriver.switchTo().alert();
System.out.println(javascriptAlert.getText()); // Get text on alert box
javascriptAlert.accept();
----> in this case I get only the text of the opened tab (from registration window: driver.getTitle();:ABC Project) but not the text of teh Info message(to see in logger.info)
3.
String winHandleBefore =
driver.getWindowHandle();
driver.findElement(By.xpath("//div[#class='col-md-4']/button[1]")).click();
// Xpath of register Button
Set handles = driver.getWindowHandles(); ..... --->>, In this case, I don't get any Window Handler of new window the window handler from old and new are the same
Additional Hint:
I am also not able to click anywhere else in Window to skip the popup window
In Google Developer tool I don't see also any source code and no Elements from the popup window (when the popups appear.
I heard from developer that this window is a javascript but I don't
see anything in source code too (it did not also work with solution b
above)
I appreciate for any tips and supports
Thanks
This picture posted looks like a javascript alert.
So the below code should have worked.
Alert alert = driver.switchTo().alert();
alert.accept();
Maybe it is a wait issue. Try WebDriverWait
Alert alert = new WebDriverWait(driver, 20).until(ExpectedConditions.alertIsPresent());
alert.accept();

How to use sendkeys on child window in salesforce using selenium webdriver?

I want to use sendkeys on a search textbox of child window displayed in these screenshots .
I'm able to find URl, and title name of child window, but whenever I want to click, sendkeys on child window, the code is halted in between with the error: org.openqa.selenium.NoSuchElementException: no such element
Please suggest how to solve above query.
hope there is no frame and if this opens in a new window, try the below:
Thread.sleep(2000);
// Store the current window handle
String winHandleBefore = driver.getWindowHandle();
// Perform the click operation that opens new window
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.findElement(By.xpath("ur xpath of that search box")).click();
driver.findElement(By.xpath("ur xpath of that search box")).clear();
driver.findElement(By.xpath("ur xpath of that search box")).sendKeys("ur text");
// Close the new window, if that window no more required
driver.close();
// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);

Opening of new tab when user click on a button provided on popup using selenium webdriver

There is a question regarding Selenium WebDriver given is a scenario
I login to the application
Click on some link
It opens a new popup which has iframe
There is a preview button on this popup on which if I click manually it opens a new tab in the previous parent window but if I click on this button using selenium webdriver it opens new window
I want to open new tab using selenium webdriver by clicking on this preview button any solution?
These are steps you should take then.
Log into application
Get current window handles
Find a list of window handles EXCEPT currentWindow handles
switchTo() newly opened window handle, switchTo() iframe
Click the button inside an iframe
Get the window handles again
switchTo() newly opened window
perform some action, close all window except parent.
A sample can look like the following. Note: C# code
public void PopupWindowHandle()
{
//logged into application
//get current window handle
string prentWindowHandle = Driver.CurrentWindowHandle;
//click should generate a new tab
Driver.FindElement(By.Id("id")).Click();
//get window handle counts. In your case should be 2
List<string> windowHandles = Driver.WindowHandles.ToList();
//switchTo newly opened window handle
foreach (string handle in windowHandles)
{
if (handle != prentWindowHandle)
{
Driver.SwitchTo().Window(handle);
//switch focus into iframe
Driver.SwitchTo().Frame(Driver.FindElement(By.CssSelector("Iframe Selector")));
//click should generate another window
Driver.FindElement(By.Id("id")).Click();
//should be 2 in total
List<string> newHandles = Driver.WindowHandles.ToList();
foreach (string newHandle in newHandles)
{
if (newHandle != handle)
{
Driver.SwitchTo().Window(newHandle);
//do some work
Driver.Close();
Driver.SwitchTo().Window(handle);
break;
}
}
Driver.Close();
Driver.SwitchTo().Window(prentWindowHandle);
break;
}
}
}
I guess you are not using chrome.:)
Usually, webdriver forces a browser to open a new window instead of a tab, it's easier to dealing with through switch window. Like IE and Firefox, but chrome will open a new tab instead of a new window and it still supports switch window. This how I found, try it out.

Save Dialog box in Silverlight

i am working on Silverlight 5,
Application has functionality like save data in user's local pc as CSV. while developing functionality it's working perfect at our local PC. when we click on "Export" Button save file dialog box appear and save at selected location. but after deployed on our server save file dialog box will not appear on screen.
dialog = new SaveFileDialog();
dialog.DefaultFileName = "Exported Data";
dialog.Filter = string.Format("File Type (*{0}) | *{0}", (".csv"));
dialog.DefaultExt = string.Format("{0}", ("csv"));
//Show the dialog
bool? dialogResult = dialog.ShowDialog();
Make sure you call ShowDialog() method right after "Export" button click event, this is Silverlight security feature.
See: http://msdn.microsoft.com/en-au/library/system.windows.controls.savefiledialog(v=vs.95).aspx
You show a save dialog control using the ShowDialog method. For security purposes Silverlight file and print dialogs must be user-initiated. This means you must show them from a user-initiated action such as the click event handler for a button. In addition, there is a limit on the time allowed between when the user initiates the dialog and when the dialog is shown. If the time limit between these actions is exceeded, an exception will occur.

Popup windows that automatically close in Selenium Webdriver

My Selenium Webdriver test is something like:
Click button -> this opens popup window
Iterate through all window handles and switch to popup window
Fill out form on popup window and submit.
At this point, the popup window might automatically close if the input was valid or it will stay open if there is an error on the form.
My question is, if the window automatically closes and that was the window handle I had focus on, what happens exactly? Is there a way for me to detect that the window has closed?
Thanks
If the focused window closed, then the Web Driver instance had no focus on any window and it throw an exception if you are trying to find any element. It throws NoSuchElementException when you trying to find an element. You can check the window presence by counting the number of windows.
The windows handles keeps the record of pop of window even if it closes automatically. So you have to switch to the parent window. And then use the print statement for Windows.title to check which window is opened.

Resources