How to close the popup dialog

I have an unexpected popup dialog “You get it first” to appear on the expedia.co.in.  It blocks my test to enter departure date and time.  I am unable to close this modal dialog by using the code below.

driver = new FirefoxDriver();
driver.Navigate().GoToUrl(“http://www.expedia.co.in/”);

var popupDialog = Driver.Instance.FindElement(By.Id(“popupmodal”));
popupDialog.FindElement(By.ClassName(“CloseBtn”)).Click();

I have also tried to use the close but can’t get it close.  Any suggestions?

driver.SwitchTo().Alert().Accept();

Train Asked on August 26, 2014 in Selenium WebDriver.
Add Comment
  • 4 Answer(s)

    Hi Carol,

    Please use below lines in your code.

    
    driver.get("http://www.expedia.co.in/");
    
    driver.findElement(By.xpath(".//*[@id='popupmodal']/div/div[1]/a")).click();
    
    
    Doctor Answered on August 26, 2014.
    Add Comment

    Hi Carol,

    You can also try this

    FirefoxDriver driver = new FirefoxDriver();
    
    driver.get("http://expedia.com");
    
    driver.findElement(By.id("popupmodal")).findElement(By.className("CloseBtn")).findElement(By.tagName("a")).click();
    

    Thanks
    Virender

    Professor Answered on August 26, 2014.
    Add Comment

    Hi Carol,

    The HTML tag of close button is this
    <div class=”CloseBtn“><a href=”javascript:toggleDiv(‘popupmodal’);” >x</a></div>

    You will see its actually two elements one a <div/> and inside it an anchor element <a/>. You will see that anchor tag has href attribute which is calling a javascript function. the anchor element is the one which is accepting the mouse click event not the div element. Hence we have to find anchor tag inside the div class and then send the click action.

    Right now there is nothing other than href as an attribute to the anchor tag, I used to to identify it. Had there been a CSS or an ID I would have used it.

    Thanks
    Virender

    Professor Answered on August 26, 2014.
    Add Comment

    Thanks Rocky and Virender,    Both solutions are working.

    A question is why the locator needs to include the tag name ‘a’?  I thought the classname ‘CloseBtn’ was unique on the popupmodal page.
    Is it because it is a popup modal and treats differently.

    Rocky –  why do you use XPath to locate the element.  I found it is difficult to understand.  Would CSSSelector be better than XPath?

    Train Answered on August 26, 2014.

    Hi Carol,

    PLease close this question if resolved.

    Regards,
    Lakshay Sharma

    on September 12, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.