bratten
bratten

Reputation: 21

Selenium code stops, because of an alert popup

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

Upvotes: 2

Views: 3293

Answers (3)

dotancohen
dotancohen

Reputation: 31481

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()

Upvotes: 0

cruisepandey
cruisepandey

Reputation: 29362

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.

Upvotes: 2

Vel Guru
Vel Guru

Reputation: 394

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/

Upvotes: 0

Related Questions