user2549379
user2549379

Reputation: 31

Selenium Webdriver - how to close the first pop up window and go to the actual page

I'm trying to automate the webpage "http://www.quikr.com",when I open this you will get a pop up window first saying "Please Choose Your Location" then after closing it , I can see the main page of quikr.

I tried closing that Popup page by automation ,but not able to do

Tried using xpath

 driver.findElement(By.xpath("//*[@id='csclose']/strong")).click();

Tried using className

 driver.findElement(By.className("cs-close cs-close-v2")).click();

Tried using id

  driver.findElement(By.id("csclose")).click();

Please help me with this

Upvotes: 3

Views: 63737

Answers (7)

Simonas
Simonas

Reputation: 1

Simple pressing Alt + F4 buttons worked for me, e.g.:

        driver.findElement(By.cssSelector("html body div div img")).sendKeys(Keys.chord(Keys.ALT, Keys.F4));

Upvotes: 0

Stormy
Stormy

Reputation: 641

Try driver.findElement(By.Id("csclose")).click(); I hope that will help

Upvotes: 0

Abhijeet
Abhijeet

Reputation: 69

The Following Code Works for Me to Handle Pop Up/ Alerts in Selenium Webdriver Just Copy Paste this Code After the Event which is triggering the Pop up/Alert i.e after clicking on save.

if(driver.switchTo().alert() != null)
{
    Alert alert = driver.switchTo().alert();
    String alertText = alert.getText();
    alert.dismiss(); // alert.accept();

}

in your case you try to run this code at starting of the code bcz it will directly close the pop up

Upvotes: 2

user2365657_Shraddha S
user2365657_Shraddha S

Reputation: 253

i have tried it in ruby and this one works see if this can help you in any way :)

require 'selenium-webdriver'
require 'test/unit'
require 'rubygems'

class Tclogin < Test::Unit::TestCase                                                        #------------ define a class----------------

    def setup
    @@driver = Selenium::WebDriver.for :firefox                                         
    @@driver.navigate.to "http://www.quikr.com"                 #---- call url----
    @@wait = Selenium::WebDriver::Wait.new(:timeout => 60) # seconds                     #----define wait------
    end

    def test_login
     @@driver.find_element(:css, "strong").click
    end
end

you can also use follwing xpath

@@driver.find_element(:xpath, "//a[@id='csclose']/strong").click

Upvotes: 1

lakshmareddy
lakshmareddy

Reputation: 21

to close multiple popups in webdriver and switch to parent window

String parent = driver.getWindowHandle();

        Set<String> pops=driver.getWindowHandles();
        {
        Iterator<String> it =pops.iterator();
        while (it.hasNext()) {

            String popupHandle=it.next().toString();
            if(!popupHandle.contains(parent))
            {
            driver.switchTo().window(popupHandle);
            System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
            driver.close();

Upvotes: 2

Code Enthusiastic
Code Enthusiastic

Reputation: 2847

public void closePopup() throws Exception {
        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://www.quikr.com/");
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("csclose"))).click();
        System.out.println("Successfully closed the start Popup");
    }

Upvotes: 0

Tedesco
Tedesco

Reputation: 880

Since this is a JavaScript modal, when the page finishes loading the JavaScript code could still be running. The solution is to wait until the button to close the modal be displayed, close it and then follow with your test. Like this:

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        wait.Until(ExpectedConditions.ElementIsVisible(By.Id("csclose")));

        driver.FindElement(By.Id("csclose")).Click();

Tested myself and works fine.

Hope it helps.

Upvotes: 1

Related Questions