How to handle Pop-up in Selenium WebDriver using Java
To switch to a popup window, you need to use getWindowHandles()
and iterate through them.
In your code you are using getWindowHandle()
which will give you the parent window itself.
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
// Now you are in the popup window, perform necessary actions here
driver.switchTo().window(parentWindowHandler); // switch back to parent window
How to Handle Popup in Selenium WebDriver
The desired element is within a Modal Dialog Box so to locate/click on the element you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
Solution
You need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
Using
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.close.js-modal-close. > span.close-icon"))).click()
Using
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='close js-modal-close ']/span[@class='close-icon']"))).click()
Note : You have to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
How to handle pop-up or alert in selenium?
First that's not popup that's an Iframe.
you can get more information from this link how to handle iframe in selenium
first you need to swich to the iframe.
for that you need to write below code after your Thread.sleep(3000);
find the iframe name from your html source.
driver.switchTo().frame("notification-frame-~2514428c7");
driver.findElement(By.xpath("//i[@class='wewidgeticon we_close']")).click();
By using this you can close your html popup.
How to handle popup window using selenium webdriver with Java
The element aContinueShopping
is contained within an iframe.
You'll have to switch to the iframe using:
WebElement frameID = d1.findElement(By.Css("#add-to-cart>iframe"));
d1.SwitchTo().Frame(frameID);
d1.findElement(By.id("aContinueShopping")).click();
There's no 'name' or 'id' on the iframe, so you'll have to use a WebElement or a numeric to find it.
Once you're done with that iframe, you'll switch back to 'top' by using:
d1.SwitchTo().DefaultContent();
Selenium Testing - Text addition via pop up window is not getting saved
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");
Login Pop-Up Using Selenium Webdriver
Then you should give it a try using explicitWait
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
//Navigate to web page
driver.get("https://url");
//Maximising window
driver.manage().window().maximize();
//Locating web element and performing the action
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("email"))).sendKeys("");
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("password"))).sendKeys("");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("submit"))).click();
//Closing browser session
driver.quit();
}
}
Related Topics
Confusion: @Notnull VS. @Column(Nullable = False) with JPA and Hibernate
Why Doesn't Java Map Extend Collection
How to Get a Date Without Time in Java
How to Turn a String into a Inputstreamreader in Java
Com.Jcraft.Jsch.Jschexception: Unknownhostkey
Use Cases and Examples of Gof Decorator Pattern for Io
How to Set Specific Java Version to Maven
Green Threads VS Non Green Threads
What Is Recommended Way for Spawning Threads from a Servlet in Tomcat
Jre 1.7 - Java Version - Returns: Java/Lang/Noclassdeffounderror: Java/Lang/Object
Deep Clone Utility Recommendation
Create Java Console Inside a Gui Panel
How to Use Java.String.Format in Scala
Using Mockito with Multiple Calls to the Same Method with the Same Arguments