2

Hey stackoverflow Community,

I have a problem. I have to test an Website with Selenium, but before the site is loaded an alert box appears. My code stops where my webdriver gets the url

public static void GoTo(string url){
    webDriver.Url = baseUrl + url;
}

The code stops in webDriver.Url = baseUrl + url; and is waiting for the input in the alertbox. I can't use the SendKeys() Function, because the code stops there and is waiting.

How can I fix this problem?

Greetings Nikolas

Hassan Radi
  • 936
  • 10
  • 22
bratten
  • 21
  • 1
  • 2
  • 1
    Possible duplicate of [Handling Alert dialog Box in Selenium IE using C#](https://stackoverflow.com/questions/45494878/handling-alert-dialog-box-in-selenium-ie-using-c-sharp) – Isma Apr 05 '18 at 11:42
  • How exactly is the _Alert Box_? Is it an _Alert_ or a _Modal Dialog Box_? Can you locate the element in the _HTML_? – undetected Selenium Apr 05 '18 at 11:44
  • It looks like this [link](https://image.ibb.co/ixb5CH/Capture.png) I can't reach the html sourcecode, because of the alert box. Can't right click the page. – bratten Apr 05 '18 at 11:51
  • Possible duplicate of [Python Windows Authentication username and password is not working](https://stackoverflow.com/questions/45328654/python-windows-authentication-username-and-password-is-not-working) – undetected Selenium Apr 05 '18 at 11:55

3 Answers3

1

I'am assuming that alert is generated by JS. In Selenium you can handle alert generated by JS using Actions class.

What you need to do is, you have to switch focus of your webDriver to that generated alert.

Alert alert = driver.switchTo().alert();
alert.dismiss();   // clicking on cancel.
alert.accept();    // click on yes.
alert.getText();   // Text to the prompt.
alert.sendKeys(charSequence args0); //sending something to that prompt.

And if you are on the same page after handling the alert, you don't need to switch to defaultContent, even if you do so there is no issue. for that you have to use :

driver.switchTo().defaultContent();

Let me know if you have any more concern about Actions class.

And if it is Window based pop up , you need to use 3rd party tools like AutoIT. Vel Guru has provided that solution for you.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • I have same issue and I have tried the Alert functions but the problem is that the execution never reaches the `switchTo().alert()` method because it is stuck on `driver.Navigate().GoToUrl(url)`. Do you know how to make the execution go further so that I actually will be able to try out this alert() method? – ASE Dec 24 '18 at 13:26
0

its Windows Authentication popup for that You can provide credentials in URL itself it means we will add username and password in URL so while running script it will bypass the same.

Example

http://username:password@url

or

use the AutoIT please refer the link for more details http://learn-automation.com/handle-windows-authentication-using-selenium-webdriver/

Vel Guru
  • 394
  • 1
  • 3
  • 16
0

Though this question is tagged C#, it came up in my Python searches as well. Here is the code to dismiss an alert in Python. Note that we are not invoking the same methods as the C# in cruisepandey's answer, but rather referencing objects.

# Invoke an alert box
driver.execute_script("alert('JS!')")

# Dismiss it
alert = driver.switch_to.alert
alert.dismiss()
dotancohen
  • 30,064
  • 36
  • 138
  • 197