Get text from alert message from web with Selenium and VBA - excel

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

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

Related

Excel Selenium VBA: Dismiss Web Selenium Pop-Up Alert

Steps:
Base URL: https://rivcoview.rivcoacr.org/#/Property-Search
Select the dropdown ""
Enter 609542017 in the Text Box
Click "GO" Button
When I am automating the above single action with a list of numbers and a loop. This Popup Alert occurs when I run a loop of PIN Numbers through, automating the "GO" click, then scaping the output page results. I am trying to dismiss the resulting popup.
No doubt, they have it triggered by some java in the background that detects my automation. I have seen several python or java possible solution examples for my problem that I can not translate to Excel VBA with Selenium. So I need a pure EXCEL VBA Selenium Solution.
CSS Selector for "X" button on the pop-up **= "#surveyModal > div > div > div.modal-header > button"
Tried:
MyBrowser.SwitchToAlert.Dismiss
Set CloseButton = MyBrowser.FindElementByCss("#surveyModal > div > div > div.modal-header > button").click
I am still happy to get suggestions on VBA Selenium PopUps, but I have solved my problem. My Issue ended up not being a pop-up problem. The Element was in the DOM all along as a hidden object. So the following code solved my problem:
Set SurveyButton = MyBrowser.FindElementByCss("#surveyModal")
If MyBrowser.FindElementByCss("#surveyModal").IsDisplayed Then
SurveyButton.Click
Else
GoTo 600
End If
600
'*******************************
I would use this as an alternative to test if the object was created. However, since it was present but hidden all along, the code below won't work and will cause an error.
Set SurveyButton = MyBrowser.FindElementByCss("#surveyModal")
If Not MyBrowser.IsElementPresent(By.Css("#surveyModal")) Then
SurveyButton.Click
Else
GoTo 600
End If
600

Hiding the form while printing - strange behavior

I have created a form with a button that allows the user to print the selected sheet. Her absolutely minimized form is below.
Option Explicit
Private Sub btnPrint_Click()
lblHideCounter = lblHideCounter + 1
Me.Hide
Application.Dialogs(xlDialogPrint).Show
Me.Show False
lblShowCounter = lblShowCounter + 1
MsgBox "bntHide_Click, Show Counter: " & lblShowCounter
End Sub
Private Sub UserForm_Terminate()
MsgBox "UserForm_Terminate, Show Counter: " & lblShowCounter
End Sub
So that the form does not disturb the user when the printing dialog box is displayed, I decided to hide it and then show it back after printing. However, I noticed a rather unexpected behavior. Well, if the form is displayed in modal mode, the code that follows the Show command of the form will be run only after the form is finally closed (even after UserForm_Terminate event). If, on the other hand, the form is displayed in modeless mode, such a problem does not occur.
For some reasons, however, I would like my form to be displayed in modal mode. However, I am concerned that this behavior may result in problems (e.g. stack utilization?), if the user repeatedly presses print without closing the mold.
Can this be solved in some other way? For example, is it possible somehow (without using the Hide method) to hide the form while the print dialog is displayed and then display it again (without using the Show method)? Or maybe I shouldn't worry about this problem?
The file with example code can be downloaded from here..

selenium no such element error element not found for excel

I know this has been asked many times, I apologize for that. but i couldn't solve the problem.
I need to export data from excel to chrome web page. but when I press a button on the web page, the small screen opens and I can't find what I'm looking for there.
to be more specific, this is the page I opened. here I click on the button marked red.
enter image description here
A screen like this opens. I need to add data here.
enter image description here
my selenium code is like this.
Public Function kursiyergiris()
Dim tc As Integer
Dim Findby As New Selenium.By
Dim tcekle As Selenium.WebElement
bot.SwitchToNextWindow
Set tcekle = FindElementById("txtTCKimlikNo") // the part where i got the error
tcekle.SendKeys ("12345678")
note: when I search, I can see the "txtTCIDID" part.
thank you for the help.
1: https://i.stack.imgur.com/SXE4T.png
2: https://i.stack.imgur.com/SjnPb.png
Actually "bot.SwitchToNextWindow" is not for the mini window in the 2nd picture. for another window, the login screen. The screen that opens does not have a name. div has name. With the code "Debug.Print bot.IsElementPresent(Findby.ID("RadWindowWrapper_ctl03"))" I can see the div. but I need to write "txtTCIDID" in it.
The 1st photo below is the main screen. The screen where I want to save the second photo. Could you please review it.
enter image description here
enter image description here
It could be that the code bot.SwitchToNextWindow is not switching to the popup window.
Can you replace that code with bot.SwitchToWindowByTitle "{title of the popup window}" and see if that works?

vba error using .Navigate to web page

folks,
i can successfully reach a web page from a UserForm within an Excel workbook and retrieve a website print image using .Navigate; i can even switch to a second web page and retrieve a website print image. but i get a 'script' error, either as i retrieve the first webpage, or as i retrieve the second web page. if i click 'yes' to continue running the script, it proceeds without issue. i don't have sufficient 'debug' experience to follow what is throwing the error. so, if you can help me find the 'root cause', maybe i can fix the error?
this attachment is the error message
the navigate module is here:
Private Sub optCandleChart_Change()
Me.wbSCC.Navigate "http://stockcharts.com/h-sc/ui?s=" & txtTicker1.Value & "&p=D&b=5&g=0&id=t56926565868&r=1501858722627&cmd=print"
End Sub
when the UserForm launches, a ticker is fed from the Excel worksheet to the UserForm in txtTicker1.Value. it works, just hitting 'Yes' to continue running script is a bit tacky. is it because i am not setting up 'Change()' properly?
thanks,
ron

Press "New mail" button in Microsoft 365 email using VBA

I am trying to send an email over Microsoft 365 using IE as the browser. I am currently having difficulty getting VBA to create a new email and am trying to do so by simply telling it to click the "New Mail" button which has the following HTML code:
<button autoid="_n_j" type="button" class="_n_j2 o365button" aria-labelledby="_ariaId_31" regionbehavior="1">
<span class="_n_l2 owaimg ms-Icon--plus ms-bcl-tp ms-bg-transparent ms-icon-font-circle ms-fcl-tp ms-icon-font-size-20-circle" role="presentation"></span>
<span class="_n_k2 ms-font-weight-regular ms-font-color-themePrimary" id="_ariaId_31">New mail</span>
</button>
I am trying to press this using the following VBA code:
Dim objButton As Object
Set objButton = IE.Document.getElementById("_n_j2 o365button")
objButton.onfocus
objButton.onclick
But am getting a run time error. I think this is because the button is using javascript (page doesnt reload when pressed, just updates). Any ideas how to do this?
Try IE.Document.GetElementsByClassName("_n_j2 o365button")(0).Click
You can't use .GetElementByID() because the button hasn't been assigned an ID.
I've used .GetElementsByClassName() because we know that the button has a class assigned, however a class is not unique. This method returns a collection of all elements with this class - without seeing the whole HTML, I'm going to make an assumption that all buttons on the page share this class.
The (0) tells the code to click the first item stored in the collection.
(Again, I'm going to assume you don't have something like Option Base 1 declared)
You might have to iterate through the collection and somehow test each item to see which button you need and change the (0) accordingly.

Resources