user3083590
user3083590

Reputation: 245

Handle a popup in selenium webdriver using c#

When I click on a button on a page, a popup is displayed. This is not a windows popup. It is the application popup.. The popup I get in my application is similar to the one i have shown in the image with a X button. now How do I move the driver control to the popup and then click on the close button available on the popup and then move back my control back to the original page..

I have to do this using Selenium WebDriver and C#.

popup sample

Upvotes: 0

Views: 17290

Answers (4)

Praveen
Praveen

Reputation: 1

You can try

driver.switchTo().frame(0);

Upvotes: 0

SiKing
SiKing

Reputation: 10329

The example you have shown is not a popup, but a simple DHTML window. To access the X of the example you have provided, you could use: driver.findElementBy(By.id("profile-tooltip-closebtn")).click().

Upvotes: 0

Sajad Deyargaroo
Sajad Deyargaroo

Reputation: 1149

You need to do the following...

  • Loop through the windows and find the desired window
  • Switch to the windows
  • Find the button in the current window and click the same

Here is the sample code in C#

foreach (string handle in browser.WindowHandles) 
        {
            IWebDriver popup = driver.SwitchTo().Window(handle);

            if (popup.Title.Contains("popup title")) 
            {
              break;
            }
        }

IWebElement closeButton = driver.FindElement(By.Id("closeButton"));
closeButton.Click();

Upvotes: 4

Mahesh Reddy Atla
Mahesh Reddy Atla

Reputation: 529

The new pop message too have an id or class name..
First get the class name or id of that pop up and the go for the xpath(may be we will find class name) of the close button and click on it.

Upvotes: 0

Related Questions