Selenium c# can’t change webdriver to alert in firefox

hi guys,
i’m writing because i have a problem with handle alert window using webdriver.

Part of the code:


FirefoxProfile firefoxProfile = new FirefoxProfile();

firefoxProfile.AddExtension("extensionPath");

IWebDriver driver = new FirefoxDriver(firefoxProfile);

driver.Url = "myUrl";

//After 5 sec alert will appear

Thread.Sleep(8000);

try

{

driver.SwitchTo().Alert();

IWebElement element = driver.FindElement(By.PartialLinkText("Allow"));

element.Click();

}

catch (NoAlertPresentException e)

{

Console.WriteLine(e.Message);

}

When the alert exist in my web browser i get


OpenQA.Selenium.WebDriverException
The HTTP request to the remote WebDriver server for URL... timed out after 180 seconds.

When it doesn’t exist program go to catch block.

My alert window:

Selenium c# can't change webdriver to alert in firefox

Could you help me?

Default Asked on January 19, 2016 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)

    Hello

     

    Within alert, you can not  use regular Selenium  API below

     

    IWebElement element = driver.FindElement(By.PartialLinkText("Allow"));
    element.Click();
    You should use
    try
    {
    alert = ngDriver.WrappedDriver.SwitchTo().Alert();
    alert_text = alert.Text;
    StringAssert.StartsWith(” your regexp here”, alert_text);
    alert.Accept();
    // and /or alert.Dismiss();
    }
    catch (NoAlertPresentException ex)
    {
    // Alert not present
    verificationErrors.Append(ex.StackTrace);
    }
    catch (WebDriverException ex)
    {
    // Alert not handled by PhantomJS
    verificationErrors.Append(ex.StackTrace);
    }

    even this may not work if the dialog is provided by Operating System, and not the browser itself. In the latter case use custom win32 / pinvoke code to manage the dialog. Let me know if you need additional help with the latter

    Train Answered on May 14, 2016.
    Add Comment
  • Your Answer

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