MAlam
MAlam

Reputation: 1

unable to close popup window while automating saksfifthavenue.com using selenium webdriver junit test

public class TestScript {

    protected WebDriver driver;


@Before
    public void setUp() throws MalformedURLException {

        driver = new FirefoxDriver();


        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
        driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);

        driver.manage().deleteAllCookies();

        driver.manage().window().maximize();

        driver.navigate().to(new URL("http://www.saksfifthavenue.com"));

    }

 @Test

    public void Test1(){



       String currentWindow = driver.getWindowHandle(); 
//even without using windowhandle the same thing happens. can't locate the popup

        WebElement closePopUp = driver.findElement(By.xpath("//div[1][@id='close-button']"));
        closePopUp.click();

    }

    @After
    public void tearDown(){

        driver.close();
        driver.quit();

    }

}

Upvotes: 0

Views: 384

Answers (2)

Sandipan Pramanik
Sandipan Pramanik

Reputation: 348

Try this code :

    // Checking Alert is present and switchTo;
    Alert alert = new WebDriverWait(driver,30).until(ExpectedConditions.alertIsPresent());
    // Dismiss alert - this will close the alert
    alert.dismiss();
    //Switching to default content after alert is closed
    driver.switchTo().defaultContent();

Upvotes: 0

OarpitO
OarpitO

Reputation: 357

The popup which is appearing is a modal and not a window, so you don't need to get windowHandle. Also, the xpath for close window is incorrect. Use id for locating the close button as follows:

WebElement closePopUp = driver.findElement(By.id("closeButton"));
closePopUp.click();

Prefer using Id and Css selectors for locating an element. Also, on a side note windowHandle is used if you need to switch to a window or get detail of that window.

Hope this helps.

Upvotes: 1

Related Questions