user3438744
user3438744

Reputation: 15

webdriver not able to click pop up window with certificate error page

Using webdriver 2.40.0 (installed from nugget packages) and writing code in C# I am - opening a link for my company website which generates a certificate error page - clicking the override link element to allow me to continue to the site - clicking an enter button on that page which generates a pop up window that also had a certificate error page on top of it My problem is when I try to select the pop up window a “noSuchWindowException” is thrown, code:

namespace webDriverDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string setURL = "xxxxx";

            IWebDriver driver = new InternetExplorerDriver(@"C:\Drivers");
            driver.Url = setURL;
            String loginPage = driver.CurrentWindowHandle;

            var securityLine = driver.FindElement(By.Id("overridelink"));
            if (!securityLine.Equals(null))
            {
                securityLine.Click();
            }

            var enterBtn = driver.FindElement(By.Id("EnterButton"));
            enterBtn.Click();

            //Select the pop up window


            driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");

            driver.SwitchTo().Window("xxxx");

I’ve tried:

driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()")

and

String riskPage = driver.CurrentWindowHandle;

and switching to that window, I’ve also tried

driver.SwitchTo().Window();

but I think the problem is that I can’t get to the window name of the certificate error page and cannot select and element on that page and try to save it as a separate handle. Really need help!

Upvotes: 1

Views: 1715

Answers (1)

Faiz
Faiz

Reputation: 3256

Once you have performed the action enterBtn.Click(); that launches the popup window, you need to then switch context to the new window (using it's window handle, not title) to be able to interact with it.

You can obtain the popup window's handle from the driver.WindowHandles list.

var riskPageHandle = driver.WindowHandles.FirstOrDefault(hwnd => hwnd != loginPageWindowHandle);

if(riskPageHandle ==null)
{
   //popup not found, log error or handle 
}
else
{
   //switch to the popup
   driver.SwitchTo().Window(riskPageHandle);
   Console.WriteLine("Popup window title is : " + driver.Title);

   //now accept the certificate error (your code, I haven't tried it)
   driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");

}

Upvotes: 1

Related Questions