'Get text from alert message from web with Selenium and VBA

I am currently creating an automation to fill-in field in the browser (Chrome) with VBA and selenium. Which is currently doing fine. (the site is internal)

However, I came across with this "message from web" prompt after saving / submitting the information. Sometimes, this pop-up box gives me a prompt that it was saved successfully and sometimes, if there are errors, the message box contains some text.

In order for me to handle this pop-up message box and put some validation. In this case, I would like to get the text inside this message box. (Please refer to the red arrow).

enter image description here

The code below is what I used in order to accept / click the "OK" button. And doesn't have any problems. However, as mentioned, I would like to get the text inside that box to put some validation.

What I tried so far:

driver.SwitchToAlert
driver.SwitchToAlert.Text <<<--- this is where I attempt to get the text however, it doesn't work.
driver.SwitchToAlert(20).accept

Also, I have check below code:

driver.switchTo().alert().getText();

However, this seems not to be working in VBA / selenium.



Solution 1:[1]

Try such a code (Change to suit your issue) Dim bot As Selenium.ChromeDriver

Sub Test()
    Dim dlg As Selenium.Alert
    Set bot = New Selenium.ChromeDriver
    With bot
        .Get "http://the-internet.herokuapp.com/javascript_alerts"
        .FindElementByCss("#content > div > ul > li:nth-child(2) > button").Click
        Set dlg = .SwitchToAlert(Raise:=False)
        Debug.Print dlg.Text
        .Wait 2000
        dlg.Dismiss
    End With
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YasserKhalil