I am trying to make the window of the selenium bot activated ...
First I declared this line
Dim mainWin As selenium.Window
then after the Get line
.Get "https://www.kuwaitcourts.gov.kw/searchPages/searchCases.jsp"
I put this line that sets the window
Set mainWin = .Window
and before a message box I would like to activate the bot window so I used these lines
mainWin.Activate
MsgBox "Click OK After Entering Captcha", 64
There is no problem at the start of the loop as the bot window is already active but as for the second loop I didn't get the bot window activated
Any idea how to overcome this point and make the selenium bot window active when needed to be active
Thanks advanced for help
Use javascript to return focus? For having window at front
bot.ExecuteScript "window.focus();"
Ways of pausing for input:
inputbox:
Why not use an inputBox (set to number type) to capture entry ? Below is pseudo code (assuming captcha is in an iframe which needs to be switched to.
Dim captchaBox As Object, captcha As Long
.SwitchToFrame (.FindElementById("someId")) '<==some identifier for iframe if present
Set captchaBox = .FindElementByCss("#txtCaptia")
' Accept Number from the user
captcha = Application.InputBox("Please enter 4 digit captcha:", "Captcha", , , , , , 1)
captchaBox.Value = captcha
'.SwitchToDefaultContent ''<back to main if in iframe and is required even after submit.
DoEvents and loop until desired length:
Code
Dim submitButton As Object
Set submitButton = .FindElementsByCss("#cmdSubmit")
Do
DoEvents
Loop Until submitButtons.count = 0
Or
Dim captcha As Object
Set captcha = .FindElementsByCss("#txtCaptcha")
Do
DoEvents
Loop Until len(captcha.value) = 4
Related
I am trying to fill one input box in web page which actually trigger events results in auto update some hidden fields.
code behind web page
<INPUT onchange=iCFieldChanged(this); tabIndex=1017 onkeypress="AllowOnly(event, '[()0-9-+]','');" onfocus="tbInitialValue=this.value;g_fFieldValue = this.value;g_fFieldId = this.id;" onblur="if(tbInitialValue!=this.value && Page_IsValid){executingPostBack=true;__doPostBack('dnn:ctr366:ClaimPhysicianInformation:_ctl2:Datapanel:_ctl0:_ctl5:ClaimRecipient:ds_ClaimRecipient:mb_ds_ClaimRecipient', '');}g_fFieldId = null;" id=dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient onpaste="return true;" style="TEXT-ALIGN: right" maxLength=10 size=11 value=9468381692 name=dnn:ctr366:ClaimPhysicianInformation:_ctl2:Datapanel:_ctl0:_ctl5:ClaimRecipient:ds_ClaimRecipient:mb_ds_ClaimRecipient>
There are 5 event listeners on the first input tag line of the code above - onblur,onkeypress,onfocus,onchange,onpaste. I can able to pass value to the input box but associated events not get released.
code tried (no errors but events not dispatched)
ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient").Value = memberId
'tried with all fire event combinations but nothing works
ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient").FireEvent ("onkeypress")
also tried to dispatch events but its showing error
Set srchbtn = ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")
srchbtn.Value = memberId
'getting error in next line
Set event_onChange = ie.Document.createEvent("HTMLEvents")
event_onChange.initEvent "keypress", True, False
srchbtn.dispatchEvent event_onChange
also tried keypress events like below
Set objEvent = IE.Document.createEvent("keyboardEvent")
objEvent.initEvent "keypress", True, False
IE.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")(0).dispatchEvent objEvent
but same error appears
error: Run time error '438' : object doesn't support this property or method
since its an secured web page I cant share the url for reproducing the code please guide me on further proceedings
Does your event of input can be fired correctly in IE without VBA? I tried to dispatch the onchange event and it worked. I followed the way in this article. You could refer to my simple sample below:
Input box: <input name="q" onchange="alert('aaa');"/>
VBA code:
Sub LOADIE()
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "http://somewebsite"
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
Set Search = doc.getElementsByName("q")(0)
Search.Value = "VBA"
Dim event_onChange As Object
Set event_onChange = ieA.Document.createEvent("HTMLEvents")
event_onChange.initEvent "change", True, False
Search.dispatchEvent event_onChange
ieA.Quit
Set ieA = Nothing
End Sub
The function above also worked with keypress event. About the error you got, you could check this thread for more information.
This might help others who are facing same issue
set srchbtn = ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")
srchbtn.Focus
srchbtn.Value = memberId
srchbtn.Blur
This question already has answers here:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
(9 answers)
Element MyElement is not clickable at point (x, y)... Other element would receive the click
(5 answers)
Closed 4 years ago.
I searched all over the internet and in this forum and can't find the answer. Below code is used to upload file to a wordpress website. When I use F8 to step through it, it works fine. However, when run the full code, after the upload using XPath ("//input[starts-with(id, 'htm15_')]").SendKeys (ws.Cells(1.4)), the macro seems to skip the next line which is to click on a button (Insert file URL) and the browser stucks on this uploading page and will not load the next page where the Publish button appears. At this point, an error came out and stated couldn't find the element Publish.
I try different things, adding global ImplicitWait, adding timeouts for the above mentioned lines of code and to no avail. It seems that after uploading the file, the macro just doesn't click the button (skips that line of code) and therefore couldn't find the Publish button in the next line. The problem lines of code have been indicated by 2 asterisks. Appreciate any help.
Public Sub UploadPost()
Dim ws As Worksheet
Dim filename As String
Dim wb As Workbook
Dim D As WebDriver
Dim Path As String
Dim i As Integer
Set ws = ThisWorkbook.Worksheets("Data")
Set D = New ChromeDriver
Const URL = "https://www.myweb.com/login/"
'To login to website
With D
.Timeouts.ImplicitWait = 10000 'global implicit wait 5 seconds than default 3 seconds
.Start "Chrome"
.Window.Maximize
.Get URL
.FindElementById("user_login").SendKeys("id").Click
.FindElementById("user_pass").SendKeys("pw").Click
.FindElementByName("submit").Click
End With
'Upload daily file to Download Monitor and get file ID
ws.Range("H2:H9").Clear
D.FindElementById("menu-posts-dlm_download").Click 'Click Download on left menu
For i = 2 To 9
With D
.FindElementByClass("page-title-action").Click 'Click Add New
.FindElementById("title").SendKeys(ws.Cells(i, 2)).Click 'Type title copy from spreadsheet
.FindElementById("_members_only").Click 'Click members only category
.FindElementById(ws.Cells(i, 3)).Click '17-china, 21-HK, 25-NAS, 10-NYSE
.FindElementByLinkText("Add file").Click 'Add File
.FindElementByXPath("//input[starts-with(#id,'dp')]").Clear 'Select the correct date
.FindElementByXPath("//input[starts-with(#id,'dp')]").SendKeys (ws.Cells(i, 9))
.FindElementByLinkText("Upload file").Click
.FindElementByLinkText("Upload Files").Click
.FindElementByXPath("//input[starts-with(#id,'html5_')]").SendKeys (ws.Cells(i, 4)) 'upload file using path from spreadsheet
**.FindElementByXPath("//*[#id='__wp-uploader-id-0']/div[5]/div/div[2]/button").Click 'Click save Insert file URL
.FindElementById("publish").Click**
ws.Cells(i, 8) = D.FindElementByXPath("//*[#id='dlm-info-url']").Value
End With
Next i
Good Morning,
I’m hoping that some kind soul out there can help me with a roadblock I’ve encountered in my quest to manipulate a website with VBA. I am using MS Excel 2010 and Internet Explorer 11.0.56.
I’m somewhat comfortable with VBA but have never used it to navigate to a website, enter information and click on buttons. I’ve managed to muddle through as follows:
In Column A of my Excel spreadsheet, I have a list of 10 digit case numbers.
The code below will open IE, navigate to the desired website, pause while I log in, then navigate to the search screen, enter in the first case number and press the SEARCH button (yes, I have the case number in this example hard coded in with no looping, but that stuff I can handle so please ignore):
Sub Button_Click()
Dim objIE As Object
Set objIE = New InternetExplorerMedium
objIE.Top = 0
objIE.Left = 0
objIE.Width = 800
objIE.Height = 600
objIE.AddressBar = 0
objIE.StatusBar = 0
objIE.Toolbar = 0
objIE.Visible = True
objIE.Navigate ("https://somewebsite.com")
MsgBox ("Please log in and then press OK")
objIE.Navigate ("https://somewebsite.com/docs")
Do
DoEvents
Loop Until objIE.ReadyState = 4
objIE.Document.all("caseNumber").Value = "1234567890"
objIE.Document.getElementById("SearchButton").Click
Exit Sub
Do
DoEvents
Loop Until objIE.ReadyState = 4
MsgBox ("Done")
End Sub
That will bring me to this screen
The file number entered in the search field will return any number of files in a dynamic table with a checkbox to the left of each file.
For this example, let’s say I am ONLY concerned with the file called “CC8” under the “Type” column. There will only ever be one instance of “CC8” for a given file number.
What I need help with is, through VBA, how do I search through this table, find the “CC8” line, and then have the checkbox to the left automatically checked?
When I inspect the “CC8” element in IE, this is the HTML associated with it (highlighted in gray; the entire table is under class “listing list-view clearfix”)
see here
The HTML for the checkbox related to the “CC8” item is below:
HTML code here
The “id” for both has the same sequence of numbers, but one starts with “viewPages” and the other “doc”.
Can anyone help me out as to what I need to add to my code to get this checkbox checked? Thank you!
Note:
Please post the actual HTML using the snippet tool.
Generally:
Without HTML to properly test, I am assuming that the following 2 nodeLists are the same length, meaning that when the search text is found in aNodeList then the assumption is the same index can be used to target the corresponding checkbox in the bNodeList:
Dim aNodeList As Object, i As Long
With objIE.document
Set aNodeList = .querySelectorAll("a[target='_blank']")
Set bNodeList = .querySelectorAll("[title='Search Result: Checkbox']")
End With
For i = 0 To aNodeList.Length - 1
If aNodeList.item(i).innerText = "CC8" Then
bNodeList.item(i).Click
Exit For
End If
Next
You could also potentially use the following instead as you say the viewPages prefixes each item:
Set aNodeList = .querySelectorAll("a[id^='viewPages']")
Other observations:
Traditional checkboxes would have a checked attribute and syntax of
bNodeList.item(i).Checked = True, but as I can't see that attribute in your element I am assuming a .Click suffices.
A colleague sent me a VBA script that runs within Microsoft Excel that, from my perspective (as someone who has never written VBA code before), appears to do a few relatively simple things:
Open up Internet Explorer, and navigate to a specific page
Fill out a few HTML form elements in that page using cells from the Excel Spreadsheet
Fire off a Javascript routine to popup a new window
Fill out a few fields on that popup window
Submit the form on the popup window
This is the code they wrote that's supposed to do that:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://myurl.com/mylandingpage"
IE.Visible = True
Application.Wait (Now + TimeValue("0:00:20"))
IE.Document.All("name").Value = ThisWorkbook.Sheets("sheet1").Range("t2")
IE.Document.All("address").Value = ThisWorkbook.Sheets("sheet1").Range("w2")
IE.Document.All("phone_number").Value = ThisWorkbook.Sheets("sheet1").Range("x2")
IE.Navigate "javascript:helpModLvl('/mylandingpage/popup_window','pop_up_form')"
Ret = FindWindow(vbNullString, "pop_up_form")
Application.Wait (Now + TimeValue("0:00:10"))
IE.Document.All("id").Value = ThisWorkbook.Sheets("sheet1").Range("H2")
IE.Document.All("new_address").Value = ThisWorkbook.Sheets("sheet1").Range("I2")
IE.Document.All("submit").Click
Application.Wait (Now + TimeValue("0:00:05"))
End Sub
For the sake of argument, assume that the landing page does have fields named name, address, and phone_number, and that the popup window has fields id, new_address, and submit, the latter of which is a proper button.
My concern is that even though they query for the popup window, they never actually use it (note that Ret is never used again after it gets assigned, and as a result, the later calls to set values for id, and so on, never actually manifest, because they're still iterating on the original webpage.
Is my understanding of this code correct? Should calls after the popup window is created be based on the handle Ret, or should I expect the code to be correct as-is?
I am writing an excel plugin that outputs an edge list and a graphml sheet that are later used by the program yED to make a bitmap of the graph for the overall output of the plugin. I am using a shell command to open the appropriate file in yED, and UIAutomation to send the commands to yED.
When there is a window of yED already open, the code executes just fine. The window is found by the polling, then set active and the commands sent over. Where it goes wrong is when the shell command causes a new window of yED to be launched. There is a splash screen for yED as it is loading in that takes a few seconds to get through, and shares the same Name and Class as the window that I am looking for. The HWND is different between the two.
My code will error out whenever there is a new window of yED launching. The error reads:
Run-time error '-2147467259 (80004005)': Automation error Unspecified
error
Reference code:
Function FindyEdByClass() As IUIAutomationElement
Dim oUIAutomation As New CUIAutomation
Dim oUIADesktop As IUIAutomationElement
Dim allChilds As IUIAutomationElementArray
Dim oUIAyED As IUIAutomationElement
Dim i As Integer
Dim Timer As Date
Set oUIADesktop = oUIAutomation.GetRootElement
Set oUIAyED = oUIADesktop
Timer = Now
RestartLoop:
Set allChilds = oUIADesktop.FindAll(TreeScope_Children, oUIAutomation.CreateTrueCondition)
Debug.Print "StartLoop" & vbCrLf;
For i = 0 To allChilds.Length - 1
'EDIT: the following line is the one that errors out.
If allChilds.GetElement(i).CurrentName = "Graph.graphml - yEd" And allChilds.GetElement(i).CurrentClassName = "SunAwtFrame" Then
Debug.Print "Found Child - yED" & vbCrLf;
Set oUIAyED = allChilds.GetElement(i)
End If
Next
If Now() > (Timer + TimeValue("00:00:10")) Then GoTo NoyED
If oUIAyED.CurrentName = "Desktop" Then GoTo RestartLoop
EndOFLoop:
Debug.Print oUIAyED.CurrentName & " " & oUIAyED.CurrentClassName & vbCrLf;
Set FindyEdByClass = oUIAyED
Exit Function
NoyED:
MsgBox "No yED Window Found"
End
End Function
The line it errors out on the If statement. Through use of Debug.Print and FindWindowEx I have discovered that the object it errors out on is not the yED window I am looking for, but the yED splash screen that precedes it. I assume that the error is caused by the splash screen disappearing after its load time is up.
How do I go about finding the window I am looking for in this case? I need to be able to find the window without seeing the splash screen that will cause the error, and I need to be able to differentiate between the two by something other than class or name.
Note: I want to compare the windows by HWND but I don't know how to find it without the class/name, and it is never the same between two runs.