简体   繁体   中英

How to handle pop-up or alert in selenium?

whenever I'm running my code, I'm getting pop-up. how to cancel that pop-up for the website : https://www.goibibo.com/

whenever I'm running my code, I'm getting pop-up. how to cancel that pop-up for the website : https://www.goibibo.com/

package basic;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class goibibo {
    private static WebDriver driver = null;
    public static void main(String[] args) throws InterruptedException  {
        // TODO Auto-generated method stub
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.goibibo.com/");
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='gosuggest_inputSrc']"))).sendKeys("A");
        Thread.sleep(1000);
        List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='dib marginL10 pad0 textOverflow width90']/div/span")));
        for (int i = 0; i < myList.size(); i++)
        {
                System.out.println(myList.get(i).getText());
                if (myList.get(i).getText().equals("Ahmedabad"))
                {
                    myList.get(i).click();
                    break;
                }
            }
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"gosuggest_inputDest\"]"))).sendKeys("Mum");
        Thread.sleep(1000);
        List<WebElement> Dept = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='dib marginL10 pad0 textOverflow width90']/div/span")));
        for (int j = 0; j < Dept.size(); j++)
        {
                System.out.println(Dept.get(j).getText());
                if (Dept.get(j).getText().equals("Mumbai"))
                {
                    Dept.get(j).click();
                    break;
                }
        }
        Thread.sleep(3000);
        driver.switchTo().alert().dismiss();
    }

}


no such alert

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.

as i can see this is the alert you are getting right

在此处输入图片说明

so you can do one thing you can write a code for an alert, if an alert is visible then press the close button surrounded by try catch so you can easily get rid of the alert when ever the alert will come it will closed ASAP and you execution will disturbed but you need to use the wait for the main elements like for from and to textfields like if you give them less wait and if the pop up takes to much time to close then test case will fail and one more thing the popup comes only one time in an one session when we are passing the source and to values

let us know if you need any more help

This is iframe which stopping you to access the element.Use webdriverwait and switch to iframe first and then access the element.Try the below code.

driver.get("https://www.goibibo.com/");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name ,'notification-frame-')]")));
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//i[contains(@class ,'wewidgeticon')]")));
element.click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM