Kishan Patel
Kishan Patel

Reputation: 1383

How to handle the pop-up as webpage and alert both simultaneously?

I am not able to fill this form and also not able to handle the alert obtain on top left corner

Please help me to fill this form and then going on to the website along with handling the alert.

Here is my code which is not working:

public class FirstCry {

public static void main(String[] args) throws InterruptedException {


            System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe");
            WebDriver a = new ChromeDriver();
            a.get("http://www.firstcry.com/");
            Thread.sleep(5000L);
            a.manage().window().maximize();

            String k = a.getPageSource();
            System.out.println(k);

            WebDriverWait Wait = new WebDriverWait(a, 30);
            Wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='amt']/div[2]/div[1]/div[1]/div[3]/div")));

            WebElement b = a.findElement(By.xpath(".//*[@id='amt']/div[2]/div[1]/div[1]/div[3]/div"));
            b.click();

        }
}

Upvotes: 2

Views: 689

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

In your website popup present inside an iframe with id iframe_Login, you need to switch that iframe before finding the close button of the popup as below :-

//Create this prefs to handle notification popup
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);

//Initialize chrome option to add prefs
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);

//Now initialize chrome driver with chrome option to handle notification alert
WebDriver a = new ChromeDriver(options)

a.get("http://www.firstcry.com/");
a.manage().window().maximize();

WebDriverWait wait = new WebDriverWait(a, 30)

//Now find iframe and switch to it
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe_Login"));

//Now find the popup close button
WebElement b = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class = '_pop_close _pop_reg_bg']")));
b.click();

//Now switch back from frame to default content for further steps
a.switchTo().defaultContent();

//Now do your further stuff 

Hope it helps..:)

Upvotes: 1

Related Questions