Run-time error while running the macro - VBA selenium chrome - vba

I am having an issue while running the code on the line "CD.FindElementById("copy_all").Click". Please see the error message in the screenshot attached Run-time error
I was wondering if there is any error handling statement which could refresh the website and continue running the code. Because I found out during debugging that if I refresh the website and I run the line "CD.FindElementById("copy_all").Click" it works.
I am using selenium and code is running on Chrome. I was thinking to use On Error GoTo but I don't know how to implement it.
Please see the part of the code below:
CD.FindElementByName("checked_lines[]").Click
Application.Wait (Now() + TimeValue("00:00:02"))
CD.FindElementById("copy_all").Click
Application.Wait DateAdd("s", 5, Now)
CD.FindElementById("requisition_line_supplier_search").Clear
CD.FindElementById("requisition_line_supplier_search").SendKeys [A5]
Application.Wait DateAdd("s", 3, Now)
Application.SendKeys "{DOWN}"
Application.Wait (Now() + TimeValue("00:00:01"))
SendKeys "(~)"
Application.Wait DateAdd("s", 3, Now)
CD.FindElementById("requisition_line_need_by_date").SendKeys [B5]
Set Inv = CD.FindElementById("requisition_line_custom_field_5")
Inv.AsSelect.SelectByValue ("2-way")
CD.FindElementByCss("div.line_bottom_inside > button").Click
MsgBox "Done!"
HTML code:
<div class="cart_header_buttons left">
<input type="hidden" name="bulk_select_all" id="bulk_select_all" value="true">
<div class="left line_num" style="width: 18px;">
<img class="icon large-lines" alt="Select lines to bulk edit" title="Select lines to bulk edit" src="/assets/icons/icon_select_column-efed5d64af8385668e233cd339d192256028673894194efb96b79255fe67c9be.png">
</div>
<button id="adjust_all" class="button small clickable-strong-disable" data-disable-on-click="true" data-disable-on-load="true" aria-disabled="false" aria-expanded="false"><span>Edit Selected</span></button>
<button id="copy_all" class="button small clickable-strong-disable" data-disable-on-click="true" data-disable-on-load="true" aria-disabled="false"><span>Copy</span>
</button>

Related

How to use getElementsBy without a specific ID

I am new to programming with Microsoft Visual Basic and am trying to create a program that will put co-ordinates into an image database website.
I am trying to put numbers into a specific space on a website.
The place that I would like to input a number for right ascension, doesn't have a specific ID when I inspect it on the website, so I tried getElementsByTagName, however, this isn't working.
I also looked to find an ID of a larger section of the HTML code as specified here: How to access HTML element without ID?, however that isn't working either.
Sub MovesData()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://wsa.roe.ac.uk:8080/wsa/getImage_form.jsp? programmeID=&database=UKIDSSDR11PLUS&ra=&dec=&sys=J&filterID=all&xsize=1&ysize=1&obsType=object&frameType=stack&mfid=&fsid=&name="
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementsByTagName("ra").Value = "23"
End Sub
The HTML code for the specific line is:
<input type="text" name="ra" value="" size="15">
The section of HTML code is:
<td nowrap="" align="right"><b>RA</b> or <b>Galactic Long.:</b></td>
<td>
<input type="text" name="ra" value="" size="15">
<td><input type="text" name="ra" value="" size="15"></td>
I expect the output to be 23 in the right ascension box but it is just blank and there is a run time error 438 "Object doesn't support this property or method". Any help would be greatly appreciated!

How to use querySelector to click a button by <a title=""> in VBA

I am trying to use a querySelector().Click to click a button in a browser.
Here is the HTML Code:
<td tabindex="0" title="Company" class="Smell" role="grid" data-table-header="some header">
<div class="classroom">
<div class="first inner classroom">
<div class="second inner classroom">
<div class="third inner classroom">
<a title="QUERYSELECTOR HERE" class="btn-link" href="/Tosomewhere/here/andthere/111">I'm not trying to grab an inner value because other tables have same inner value</a>
</div>
<div class="different sub-classroom">
<span class="some span">Mrs. Crobapole</span>
</div>
</div>
</div>
</div>
</td>
Here is what I tried:
ie.Document.getElementById("Input Simpson").Value = ActiveCell.Value
ie.Document.getElementById("Some button for Simpson").Click
Do
DoEvents
Loop Until ie.ReadyState = 4
ie.Document.querySelector("[title='QUERYSELECTOR HERE']").Click
Do
DoEvents
Loop Until ie.ReadyState = 4
This is the error message I get:
Run-time error '424': Object required
The debug highlights this portion of the code
ie.Document.querySelector("[title='QUERYSELECTOR HERE']").Click
Yes, I did assign an object name and its preceding codes work perfectly fine. But if I click the "debug" button and run the code again, it works strangely.
try catching the object by tag index, on your case index of tag <a>, you can loop between the html elements to get the one you need according the attribute on your case title="QUERYSELECTOR HERE"
; this code loops between <a> tags inside ("td")(0) and search for the title attribute.
Dim MyHtmlTable As Object
MyHtmlTable = ie.document.getElementsByTagName("td")(0)
For i = 0 To MyHtmlTable.All.tags("a").Length - 1
If MyHtmlTable.getElementsByTagName("a")(i).getAttribute("Title") = "QUERYSELECTOR HERE" Then
MyHtmlTable.getElementsByTagName("a")(i).Click
Do
DoEvents
Loop Until ie.readyState = 4
Exit For
End If
Next i
hope this helps.

Simulate Click event on a DIV element

I cannot simulate a click event on a div element. The HTML code is this one:
<div id="export" data-export="true" data-timespan="">
<button class="btn btn-default ladda-button" type="button" data-style="expand-right" data-spinner-color="#8899a6">
<span class="Icon Icon--featherDownload"></span>
<span class="ladda-label">
Exportar datos
</span>
<span class="ladda-spinner"></span></button>
<div class="hidden error server Callout Callout--danger">
<p>Ocurrió un problema al exportar tus datos, inténtalo de nuevo más tarde.</p>
</div>
</div>
In the webpage, is just a button that downloads a file when clicked. There is no URL. But after checking the code, is the div element what triggers the click event. Look the image.
Click to see image
That "g.handle" triggers a js.file form the website.
My vba code works perfect for doing other stuff in the website, but I cannot accomplish this task.
I've tried all this options:
Set div = HTMLDoc.getElementById("export")
div.FireEvent ("onclick")
div.Click
HTMLDoc.all.Item
Call HTMLDoc.parentWindow.execScript("g.handle", "JavaScript")
Nothing works. I'm kind of blocked right now and I have no idea of how to proceed. Maybe this has no solution? I have almost no idea of HTML and I have 0 idea of Javascript. But it must have a solution, because if I do it manually, i click on that DIV, wait some seconds and download my file.
Thanks in advance. Any help is welcome.
You didn't specify if the highlighted portion was the html code for the button or not, but it appears from looking at it that it isn't.
You can grab the class name for your button. The name may or may not be unique, and it's difficult to know that without seeing the entire html code, but you can set the btn variable below to the first instance of that class name. If there are multiple class names in your html code, then you will need to use a For Each...Next statement to iterate through them (unless you know the instance number off hand).
So we will go with the first instance of your class, hence the reason for the (0) appended on the end.
Sub Test()
Dim ie As New InternetExplorer, HTMLDoc As HTMLDocument, btn As Object
ie.navigate Url 'whatever this may be
' Some code to wait for your page to fully load
Set HTMLDoc = ie.document
Set btn = HTMLDoc.getElementsByClassName("btn btn-default ladda-button")(0)
btn.Click
End Sub

Use VBA to click on a button in IE

I'm currently looking for a way to click on a button on a web page to automate an export.
I've already succeeded with logging in to the web page and clicking on the login button to redirect the page, but then I could not click on the next button i'm looking for.
My code :
For Each ele In IE.document.getElementsByTagName("ul")
If ele.document.getElementsByTagName("a").getAttribute("aria-labelledby") = "Exporter vers CSV" Then ele.Click
Next
Website source:
<ul class="dropdown-menu pull-left">
<!-- ngRepeat: item in secondaryItems track by item.dataAid --><li ng-repeat="item in secondaryItems track by item.dataAid" class="">
<a role="button" ng-show="item.visible" aria-labelledby="Exporter vers CSV" data-aid="tool-bar-inner-dd-btn-export" type="submit" ng-disabled="!item.enabled" ng-class="{plToolbarItemDisabled:!item.enabled, disabled:!item.enabled}" class="grid-export-item-btn" ng-click="item.callback(item, $event)" pl-toolbar-button-in-dropdown="" item="item">
I cannot test it without knowing your URL, but this maywork:
For Each ele In IE.document.getElementsByTagName("ul")
For Each ele2 In ele.getElementsByTagName("a")
If InStr(ele2.InnerHtml, "aria-labelledby=""Exporter vers CSV""") Then ele2.Click
Next ele2
Next ele
Give this a go. When you are upon instr() function then simply the below method should get you there.
For Each ele In IE.document.getElementsByTagName("a")
If InStr(ele.getAttribute("aria-labelledby"), "Exporter vers CSV") > 0 Then ele.Click: Exit For
Next ele

VBA with selenium throws element not visible error

Upon running my script , I get stuck with element not visible error in vba with selenium. Here is the code I was trying with:
Sub Table_stuff()
Dim driver As New WebDriver
With driver
.Start "chrome", "http://apps.tga.gov.au/Prod/devices"
.get "/daen-entry.aspx"
.Timeouts.PageLoad = 20000
.FindElementById("disclaimer-accept").Click
.Timeouts.PageLoad = 20000
.FindElementById("medicine-name").SendKeys ("pump") ''Error thrown here
.FindElementById("medicines-header-text").Click
.FindElementById("submit-button").Click
.Timeouts.PageLoad = 20000
End With
End Sub
Here is the element reaching where my scraper throws that specific error:
<input type="text" name="medicine-name" id="medicine-name" value=""
placeholder="Type at least 3 characters" title="Enter medical device name"
autocomplete="off" maxlength="100" class="placeholder" style="color: rgb(0, 0, 0);">
From what I understand Timeouts.PageLoad call would not actually trigger selenium driver to wait - I think it just sets a page load timeout. Which means that your code would try to send keys to the search input at the moment of the opened license dialog - which triggers the "element not visible" error.
What you need is a Wait call (well, ideally an Explicit Wait, but I am not sure if VBA bindings have that functionality):
.Wait 3000 ''3 seconds