7

I want to read the text from the alert box.

If this text is found in the alert box I have to close the alert box:

Alert Box

halfer
  • 19,824
  • 17
  • 99
  • 186
venkat
  • 1,203
  • 3
  • 16
  • 37
  • Do you have something? (Code) – Ratmir Asanov Nov 02 '17 at 11:49
  • @RatmirAsanov, alert = driver.switch_to_alert() I tried this – venkat Nov 02 '17 at 11:50
  • How would you do it in a non-automated environment? Is that an alert box or simply another element on the page that gets populated/visible on a certain condition? If it's a simple alert box, you could simply wait for it to appear, switch to it, get its text property and then dismiss it. If it's a div element, you might not be able to access the text of an inner element directly and you might have to fetch its parent div first then iterate through that div's children searching for the proper element that houses your alert text. – BoboDarph Nov 02 '17 at 11:59

4 Answers4

8

To read the text from the Alert Box, validate and close the Alert you have to switch to the Alert first and follow the below mentioned steps:

alert = chrome.switch_to_alert()
alert_text = alert.text
# validate the alert text
alert.accept()

However, now it seems switch_to_alert() is deprecated. So as per the current implementation you need to use:

  • switch_to.alert() as follows:

    alert = driver.switch_to.alert()
    alert_text = alert.text
    # validate the alert text
    alert.accept()
    
  • As per best practices you should always induce WebDriverWait for the alert_is_present() before switching to an Alert as follows:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    # other lines of code
    alert = WebDriverWait(driver, 5).until(EC.alert_is_present)
    alert_text = alert.text
    # validate the alert text
    alert.accept()
    

You can find a relevant discussion in Why switching to alert through selenium is not stable?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
3

First of all, you should switch to the alert window:

alert = driver.switch_to_alert()

Then get the text on the alert window by using alert.text. And check your text for correctness.

Then do such action (close the alert window):

alert.accept()
halfer
  • 19,824
  • 17
  • 99
  • 186
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
2

I have similar situations as this in my framework as well, and this is how I solved it.

if (_driver.FindElement(By.XPath("//*[text()[contains(.,'No Sales Found')]")).Enabled)
{
     //Do Something
}

Put this after functionality that may bring the error up. Also, this example is using C# and _driver as the driver, which may be different from what you are using.

JOberloh
  • 1,026
  • 2
  • 11
  • 20
0

I dont know python but in java you can like this:

Alert alert = driver.switchTo().alert();
String msg=alert.getText();
if(msg.equals("Message you compare"))
{
alert.accept();
}
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36