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();
Related
I am trying to login to Flipkart and make a purchase. But as soon as i click on the Login/SignUp button, a pop up comes suggesting existing usernames and has a Cancel button.
I am unable to find code for Cancel button through Inspect. How can i handle this?
To interact with the new window you have to change the element with which Selenium interacts, you can use the switchTo method and then indicate the name of the window:
driver.switchTo().window("windowName");
If you do not know the name of that popup that appears to you, you can get all the windows and look for the cancellation button:
for (String window : driver.getWindowHandles()) {
driver.switchTo().window(window);
List<WebElement> elements = driver.findElements(By.id("cancel"))
if (!elements.isEmpty()) {
elements.get(0).click();
break;
}
}
After login my main window disabled and one popup window showing. I tried to switch window, directly clicking cancel element on alert but none of them working.
I attached image with html code and popup window for your reference.
As per screenshot, cssSelector having 2 matching nodes, so have a look for specific Cancel element. It may pointing to wrong element.
If you have correct locator then try with
waits, if not works Thread.sleep helps sometime to simulate click properly,
As HTMLcode is not clear, check if Cancel element is inside iframe/frame or not,if it is inside a iframe/frame then switch to frame,
As we are seeing HTML code, so it is not Alert, check in HTML instead of Firepath to see if it is new windows or try getWinddowHandles, get size of set, to see as it is window or not
Set<String> handles = driver.getWindowHandles() ;
We can try, switch to active element also
If webdriver click is working, you can try click by using javascriptexecutor
WebElement element1 = driver.findElement(By.xpath("//elementpath"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", element1);
I need to accept a web browser popup which comes on refreshing the website.
I have used following script to refresh the web site -
driver.FindElement(By.Id("all_bt")).SendKeys(Keys.F5);
After that I used below code to accept the web browser popup.
IAlert alert = driver.SwitchTo().Alert();
Thread.Sleep(3000);
alert.Accept();
But I am getting error as Alert not found.
I believe the focus is not on the popup, that may be the reason.
Can anybody help me on this?
If you need to refresh the page, instead of sendkey with F5 you can try as below in Java
driver.navigate().refresh();
Regrading popup, are able to inspect elements in that popup? if you are able to inspect popup then it will be normal page not Alert.
Cross check the locator used, try with required wait to appear that ok button and is that button inside any frame or not (iframe). if it is inside frame then switch frame before click. After work is done in frame then switch back to default content.
driver.switchTo().frame("provide frame name or location"); //switch to frame command
driver.switchTo().defaultContent(); //switch to default content command
//below is wait command
WebDriverWait wait=new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id of ok")));
Thank You,
Murali G
Try directly click on ok .. if still not work then check if there is any frame present .. it must a application pop-up besides browser pop-up .. switch to alert won't work in that case
Hope it will help you :)
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.
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.