I use selenium 2 and the browser version is IE 11. I face issue while handling with pop up.
Scenario 1:
driver.findElement(By.id("I create modal window")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
I get the output as 2.
Scenario: 2 (with the same concept)
driver.findElement(By.id("I create pop up")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
I get the output as 1.
I could not switch to the pop-up. Some times the pop-up response like not connected and moreover the size of the popup is way smaller than what it should be.
What would be the problem
Note : The element id's are not real one
You have to first check its window or alert.
Simply try to inspect element in that popup, if you are able to inspect elements in popup then its window so use switch to window command, if not then it is alert. So use switch to alert command (which provided in another answer)
From your question it looks like you wanted to switch to a pop - up.
Window handler is used to switch between browser windows.
Alert handler is used to switch between the pop - up messages.
If you wanted to accept/yes/ok the alert:
Alert alert = driver.switchTo().alert();
alert.accept();
If you wanted to dismiss/no/cancel the alert:
Alert alert = driver.switchTo().alert();
alert.dismiss();
There are different types of alerts and it should be handled differently as given below.
Windows type:
if your popup opened in a new window, you can handle it using get window handle and switch to window methods.
driver.selectElement(By.id("I create modal window")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
for( String window : windows)
driver.switchToWindow(window);
Javascript alert:
if the popup is javascript type. you can use switch to alert method.
Alert alert = driver.switchTo().alert();
DOM Element type:
sometimes the popup is like DOM Element, created dynamically or hidden until popup. It can be handled using find elemeent method.
driver.findElement(locator).Click();
I haven't fix my problem, but I found interesting things : when i open a popup, it is not connected to the current session if i open my web site with Selenium, even if don't ask Selenium to open my popup but i do it my self. If id do :
driver.quit()
the Main window will be close but not the popup
Related
I used below selenium codes to click "OK" alert box, But It is not identifying, I dont knw wht's the reason, Can anyone help to solve this Issue ?
Element image:
Code trials:
Alert click_on_update_button_Htwelveyes = driver.switchTo().alert();
click_on_update_button_Htwelveyes.accept();
}
Console Output:
org.openqa.selenium.NoAlertPresentException: no such alert
(Session info: chrome=103.0.5060.134)
Alert
An alert box is used if the user wants to make sure information comes through to the user. The user will have to click on OK to agree or Cancel to deny the information.
This usecase
The following element doesn't seems to be an alert but a Modal Dialog Box :
In such cases you need to locate the element with the Modal Dialog Box to locate the element with text as Yes or No and invoke click() on the respective element of youe choice.
I am trying to automate a website using selenium web driver.
On the website, there is a popup form for creating a gate name with a text field to enter the text. Using selenium I tried to do, the pop up came but the given text was not getting saved. To pop up it's working correctly. Need guides to complete the action.
Methods I used are given below:
Alert alert=driver.switchTo().alert();
driver.switchTo().alert().sendKeys("New Gate");
alert.accept();
System.out.println(alert.getText());
Elements are identified by the Xpath element locator.
I also gave text along with URL, another method that I got from the stack.
If its an alert then right way handle as per your requirement steps should be
switch to and alert
enter text
get entered text
accept
Alert alert=driver.switchTo().alert();
alert.sendKeys("New Gate");
System.out.println(alert.getText());
alert.accept();`
If its a model popup then try to locate popup element and directly used send keys
sample code :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_popup_locator));
driver.findElement(By.you_textbox_locator).sendKeys("expected text");
You need to switch to pop-up first. Then you can access elements in the pop-up.
// save your main window handle
String MainWindow=driver.getWindowHandle();
// Get all window handle
Set<String> handles = driver.getWindowHandles();
Now, for each childHandle in handles,
driver.switchTo().window(childHandle );
// check if your textField present.
// If found, do your actions and break;
// else switch to next window
At last, switch to main window again.
driver.switchTo().window(MainWindow);
I need to close a dialog box that pops up after I had selected 'Save' by clicking 'Ok', however none of the elements on the page as well as dialog box can be inspected by right clicking after the pop-up (tried F12 doesn't help).
Alternatively, to close the dialog box enter key could be given, however I'm unable to send the enter key as listed below.
Actions action = new Actions(driver); //attempt 1
action.sendKeys(Keys.RETURN);
action.sendKeys(Keys.RETURN).perform(); //attempt 2
Both actions does not close the dialog box. I had performed the driver switch as well. In addition to the dialog box pop-up there's also an outlook email pop-up could this create an issue in identifying the alert pop-up? Kindly advise on how enter key could be passed to close the dialog box.
Found the issue, it was due to Thread.sleep(3000) that I had used after the click, guess this caused it to miss the alert from being recognized. I might be worng as well. Thanks for all your help!
As none of the elements can be inspected by right clicking after the pop-up which indicates its an Alert which is Javascript generated and you can use the following line of code :
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
myAlert.accept();
Update A
As per the comment update :
Observation : In addition to the alert msg there's also outlook mail also pops up
Conclusion : If outlook mail pops up possibly it's not an Alert as suspected. You should be able to track the element within the HTML DOM.
Update B
As per the comment update :
Observation : there are 2 thing happens when i click on 'Save': [1]: Outlook email pop-up [2]: Alert notification on the saved page stating the records are being updated
Conclusion : Sounds like two window_handles opening up, treat them as window_handles.
Solution 1 : Try switching to the pop up and handle it.
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Solution 2 : If you want to go with Keyboard keys.
Actions action = new Actions(driver);
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
action.sendKeys(Keys.RETURN).perform();
In both cases, use 'wait' until you get an alert.
First, check the dialog box that pops up after you click 'Ok' is JavaScript Alert or anything else?
If the dialog is JavaScript Alert then try below code.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
I'm trying to check whether the popup window I want to open is opened or not.
I have checked some question answers like
How would you check if a popup window exists using selenium webdriver?
But, nothings helped to solve the problem.
Here, first I open the login window by clicking the login button.
driver.findElement(By.xpath("//a[#id='login_btn']")).click(); // Click Login Button
I even tried getPageSource() but, it seems not working.
Any kind of help would be appreciated.
Thanks in advance. :)
If it's a native, browser alert (= a popup) you can do the following:
try{
driver.switchTo().alert();
// If it reaches here, it found a popup
} catch(NoALertPresentException e){}
What it looks like you're actually dealing with is an iframe which you can do the following after getting the attribute value of the "iframe" attribute:
driver.switchTo.frame("ValueOfIframe");
// Treat as normal webpage. Now in iframe scope
driver.switchTo.defaultContent(); // To return back to normal page scope
String mwh=driver.getWindowHandle();
Now try to open the popup window by performing some action:
driver.findElement(By.xpath("")).click();
Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows
Iterator ite=s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mwh))
{
driver.switchTo().window(popupHandle);
/**/here you can perform operation in pop-up window**
//After finished your operation in pop-up just select the main window again
driver.switchTo().window(mwh);
}
}
I am currently working on creating a script that will test the functionality of a website. Currently i'm blocked by a popup message that appears when certain conditions are met, when that popup message appears my script fails, if i add something like driver.findElement(By.xpath("//div[4]/div/div/div/div/div/button")).click();
the script fails when the popup does not appear.
My question is: Is there a way to check "If the button exists then click button x if not move forward". I know after what action (click) the message appears/does not appear.
Keep in mind that i'm now learning java and selenium.
Banu
You are right, You should use if else condition.
Because you exactly know in which condition pop up appears and you can use pop up text in if condition.
By
driver.switchTo().alert().getText();
Use Alert class to handle Popups
Alert alert = driver.switchTo().alert();
alert.accept();
You can use explicit wait for presence of button:-
WebDriverWait wait = new WebDriverWait(driver,10)
WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("---")));
Then to dismiss/accept the alert message use following code:-
driver.switchTo().alert().dismiss();
driver.switchTo().alert().accept();