I am trying to access a webpage and update the user name and password, but unable to get the getElementById
I tried the similar code with google and yahoo, they work perfectly. Below is the code I'm using
Sub Openurl()
Dim IE As Object
Dim IEPage As Object
Dim IEPageElement As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate URL
' The below piece of code also does not work
Do Until IE.readyState = 4 'READYSTATE_COMPLETE in early binding
DoEvents
Loop
Set IEPage = IE.Document
Set IEPageElement = IEPage.getElementById(cwsUID)
If Not IEPageElement Is Nothing Then
IEPageElement.Value = UserName
Set IEPageElement = Nothing
Else
MsgBox "Coould not find the '" & UNElementID & "' element ID on the page!", vbCritical, "Element ID Error"
Exit Sub
End If
End Sub
Kindly let me know if there is something wrong with this code.
Below is the source code
input type="text" name="cwsUID" id="cwsUID" value="" placeholder="CWS ID" autocomplete="off" autofocus=autofocus
Although I am not VB-expert, I believe you have to use quotes:
IEPage.getElementById("cwsUID")
as cwsUID is not a variable name but id.
I was able to get the GetElementID by using
Set IE = New InternetExplorerMedium
Related
Hello dear VBA collegues :)
Sub login()
'test
Const URL$ = "https://kwm.kromi.de/cgi-bin/kwm?HTML=frontend/login.htm"
Dim UserName As String, Password As String, LoginData As Worksheet
Set LoginData = ThisWorkbook.Worksheets("Sheet1")
UserName = LoginData.Cells(1, "B").Value
Password = LoginData.Cells(2, "B").Value
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.Navigate URL
ieBusy IE
.Visible = True
Dim oLogin As Object, oPassword As Object
Set oLogin = .document.getElementsByName("VS_LOGIN")(0)
Set oPassword = .document.getElementsByName("VS_PASSWORD")(0)
oLogin.Value = UserName
oPassword.Value = Password
.document.forms(0).submit
ieBusy IE
Stop
'.document.getElementsByTagName("a")(2).href
'.document.getElementsByClassName("link3").Click
.Navigate2 ""
ieBusy IE
Stop
End With
'''
End Sub
Sub ieBusy(IE As Object)
Do While IE.Busy Or IE.readyState < 4
DoEvents
Loop
End Sub
And the first task is work, macro log in to website. I need to go deeper and click something but structure of web is too much for my small head I am looking some examples on website but nothing work. I showed code of website below. I need to click button "statystyka".
/html/body/div[1]/div[1]/a[2] - Xpath adress
[link picture]https://ibb.co/2Pgx2tn
May you give me some help please :)
edit:
I tried use something like this:
'.document.getElementsByTagName("a")(2).href but this not good way on thinking
You need to move into the appropriate frame, add a wait as I am using .Navigate to the frame src, then you can target by a substring of the onclick attribute:
ie.navigate ie.document.querySelector("[name=Navigator]").src
ieBusy ie
ie.document.querySelector("[onclick*=statistic]").click
If You want navigate by tag You need to set frame as is below
Dim doc As HTMLDocument
Dim doc2 As HTMLDocument
Dim lnk As HTMLLinkElement
Set doc = IE.document
Set doc2 = doc.frames("Navigator").document
Set lnk = doc2.getElementsByTagName("A")(1)
lnk.Click
#QHarr #Raymond Wu :) Thank You for trying help, maybe that will be solution in others
I am working on a code that uses VBA-Excel to navigate to a website and automate a daily task i do . Having said that , am stuck in between of trying to delete an html element . The following js code works perfect i.e. :-
var elmnt = document.getElementById("foregxyz");
elmnt.remove();
How could i make the above 2 lines of js code in vba excel ?
I tried the following but with no result . Please help , thanks in advance .
Sub DailyTask()
Dim ie As InternetExplorer
Set ie = New InternetExplorerMedium
Dim url As String
Dim elementToBeDeleted As String
url = "https://www.example.com"
ie.Visible = True
ie.Navigate url
While ie.Busy
DoEvents
Wend
ie.Document.getElementsByTagName("select")("toDOList").Value = "TEST1"
ie.Document.getElementsByTagName("select")("scheduleDate").Value = "TEST2"
ie.Document.getElementsByTagName("select")("descriptionTask").Value = "TEST3"
'Many more of data entry until submitting the form works fine .
elementToBeDeleted = ie.Document.getElementById("xyz")
Call DeleteTagById(elementToBeDeleted)
Set ie = Nothing
End Sub
Function DeleteTagById(elementToBeDeleted As String) As String
On Local Error GoTo MyError
Dim HTMLDoc As HTMLDocument
Dim Node As IHTMLDOMNode
If elementToBeDeleted = "" Then GoTo MyExit
Set HTMLDoc = New HTMLDocument
Set Node = HTMLDoc.getElementById("xyz") 'Node always equals to nothing , why ? hence i get stuck here and cant move further.
'If Node Is Nothing Then GoTo MyExit
Node.ParentNode.RemoveChild Node 'this line gives me run time error 91 object variable or with block variable not set .
MyExit:
Set Node = Nothing
Set HTMLDoc = Nothing
Exit Function
MyError:
'Handle Error
Resume MyExit
End Function
Help Everyone,
I have VBA code to open up the report picture as shown below, it will select ERX IE WORKFUSION HOA from the first drop-down list, click View Report (as that is the only way to show the options in the User drop-down box), and then I want it to select "Select All" from the User list. Unfortunately, the User list is not created the same way in HTML as the User Group one, so I cannot mimic what I've done there.
I am getting a Run-time error 91: Object variable or With block variable not set error on the following line near the bottom .document.getelementbyId("ct132_ct104_ct107_divDropDown_ct100").Click
Sub Import_DCTR_Bot_Prod()
'Set Variables
Dim IE As InternetExplorerMedium
Dim targetURL As String
Dim webcontent As String
Dim sh
Dim eachIE
targetURL = "http://lousrswpl10:8081/Reports/Pages/Report.aspx?ItemPath=%2f12717%2fReports%2fWorkflow%2fePostRx+Detailed+Audit+Report&ViewMode=Detail"
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.navigate targetURL
'Navigate to ePostRx Detailed Audit Report
While IE.Busy
DoEvents
Wend
Do
Set sh = New Shell32.Shell
For Each eachIE In sh.Windows
If InStr(1, eachIE.LocationURL, targetURL) Then
Set IE = eachIE
Exit Do
End If
Next eachIE
Loop
Set eachIE = Nothing
Set sh = Nothing
While IE.Busy
DoEvents
Wend
'Select ERX IE WORKFUSION HOA from User Group
With IE
.Document.getElementById("ctl32_ctl04_ctl05_ddValue").Value = "42"
.Document.Forms(0).submit
End With
With IE
.Document.getElementById("ct132_ct104_ct107_divDropDown_ct100").Click
End With
End Sub
Here's what the HTML says on the actual Select All checkbox:
<INPUT onclick="$get('ctl32_ctl04_ctl07').control.OnSelectAllClick(this);" id=ctl32_ctl04_ctl07_divDropDown_ctl00 type=checkbox value="" name=ctl32$ctl04$ctl07$divDropDown$ctl00>
I am trying to input data from Excel workbook to web pages search field and click search button. A lot of different approaches has been tested with success. Current solution gives 424 Error, Object required. Pointing to:
.Document.getElementById("crmGrid_findCriteria").Value = _
Sheets("Other data").Range("A2").Value
HTML for search field:
<input id="crmGrid_findCriteria" title="Search for records" hintlabelid="crmGrid_findHintText" type="text" class="ms-crm-Dialog-Lookup-QuickFind" maxlength="100" tabindex="0" value="">
HTML for Search button:
<img id="crmGrid_findCriteriaImg" src="/_imgs/imagestrips/transparent_spacer.gif" class="ms-crm-ImageStrip-search " imgbase="/_imgs/search" title="Start search" alt="Start search">
Here is structure:
Here is my VBA (link for CRM system so it is not public):
Sub GoToOpport()
'This will load a webpage in IE
Dim ieappIE As InternetExplorerObject
Dim HWNDSrc As Long
Dim elementsSet AsappIE Object= CreateObject("internetexplorer.application")
Set ie = Nothing With appIE
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.Navigate "https://crm.dynamics.com/main.aspx?area=nav_oppts&etc=3&page=SFA&pageType=EntityList&web=true"
With ie
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
End With
ie.Document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria").Value = "hello"
'Unload IE
Set ieappIE = Nothing
End Sub
Error:
It is in an iframe so you need to access that
ie.document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria")
You can use js to attempt to assign value
ie.document.parentWindow.execScript "document.getElementById('contentIFrame0').document.getElementById('scrmGrid_findCriteria').value = 'hello';"
You could also try navigating to the src of the iframe
ie.navigate2 ie.document.querySelector("#crmContentPanel").src
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
With ie.document.getElementById("crmGrid_findCriteria")
.focus
.value = "abc"
End With
I'm getting a 301 Moved Permanently status when I send a GET request and then I'm redirected here:
https://dynamics.microsoft.com/en-us/crm/what-is-crm/
The id="crmGrid_findCriteria" is nowhere to be found in the HTML response, so my guess is that if you try:
debug.print ie.Document.getElementById("crmGrid_findCriteria") Is Nothing
you'll get TRUE as a result.
In other words, the element you're looking for doesn't exist in the HTML you downloaded.
I am trying to make a VBA that can read the HTML and checked if a specific check box is checked and write either check or unchecked in a cell. But I am having difficulties with VBA as I do not use it as often, any advise will be appreciated.
HTML
<input id="foo1" type="checkbox" name="Device" value="iPad"
checked="checked">
VBA
Sub getValue()
Dim IE As Object: Set IE =
CreateObject("InternetExplorer.Application")
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim Country As String
With IE
.Visible = False
.navigate ws.Range("A3").Value
Do
DoEvents
Loop Until .readyState = 4
End With
Set oShell = CreateObject("WScript.Shell")
Dim document
document.getElementById("checkBox(iPad)")
Item(0).Checked = True
End Sub
Try
Debug.Print ie.document.querySelector("#foo1").getAttribute("checked") ="checked"
I am not sure, without an URL to test with whether there is .Checked property you can evaluate for True ( ie.document.querySelector("#foo1").Checked)
Without more HTML hard to say if this will be able to access the required element. There may be forms/iframes/frames to negotiate.
This can also be done with looping all the input elements on the website until you find the one with the right name, this will of course be slower then a querySelector, but can be usefull if you need to change multiple input elements.
Dim objCollection as Object
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
'Loop through all elements and find the checkbox
While i < objCollection.Length
If objCollection(i).Name = "Device" Then
objCollection(i).Checked = False
End If
i = i + 1
Wend
If you only have 1 checkbox i would no doubt go with a querySelector as #QHarr