I am currently trying to click on a specific img button on a site that does not have an Id so I am trying to click based on the alt. Below is from the DOM Explorer, the alt is in Japanese characters.
<a href="/e-navi/members/statement/index.xhtml?tabNo=0">
<img width="112" height="23" alt="翌月以降" src="https://image.card.jp.rakuten-static.com/r-enavi/WebImages/enavi/common/tab05_after_o.gif">
</a>
The code I am using to try and click the link is:
pUnicodeString = ChrW(32716) & ChrW(26376) & ChrW(20197) & ChrW(38477)
Set tags = ie.document.getElementsByTagName("a")
For Each tagx In tags
If tagx.getAttribute("alt") = pUnicodeString Then
tagx.Click
End If
Next
NOTE: I have also tried it with Set tags = ie.document.getElementsByTagName("img") as well.
When running the code I get the "Permission Denied" error on If tagx.getAttribute("alt") = pUnicodeString Then.
I have been searching on many sites and have tried many peoples past examples but none worked. Below is my full code for reference.
Sub csvfetch()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim div As HTMLDivElement
Dim url As String
Dim form As Variant
Dim button As Variant
Dim tags As Object
Dim tagx As Object
url = "https://www.rakuten-card.co.jp/e-navi/?l-id=corp_de_enavi_index_001"
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate url
While .Busy
DoEvents
Wend
End With
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
'temp user/pass
user = Sheets("Pass").Cells(2, 2)
pass = Sheets("Pass").Cells(2, 3)
ie.document.getElementById("u").Value = user
ie.document.getElementById("p").Value = pass
'login
Set form = ie.document.getElementsByTagName("form")
Set button = form(0).onsubmit
form(0).submit
While ie.Busy
DoEvents
Wend
Set ElementCol = ie.document.getElementsByClassName("rf-button-alt rce-white-button rf-mini")
ElementCol.Item(0).Click
While ie.Busy
DoEvents
Wend
pUnicodeString = ChrW(32716) & ChrW(26376) & ChrW(20197) & ChrW(38477)
Set tags = ie.document.getElementsByTagName("a")
For Each tagx In tags
If tagx.getAttribute("alt") = pUnicodeString Then
tagx.Click
End If
Next
Set ie = Nothing
'Unload UserForm1
End Sub
Related
I tried automating a task on my CRM (using VBA on IE) where there is a field that prompts a suggester as soon as you start typing in it. Even if I enter the exact intended text, the script doesn't save the changes.
Here is the HTML code of the field to be edited:
<input class="itext" size="40" id="txtStrPoblacion" onkeyup="searchPoblacion(this.form, true);" autocomplete="off" value="Sector 14, Gurgaon, Gurgaon" type="text">
And, here is the script that I wrote:
Sub L3()
Dim IE As Object
Dim i As Long
Dim doc As HTMLDocument
Dim oHTML_Element As IHTMLElement
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
For i = 2 To 2
IE.navigate "https://in.nuptic.com/admin/admin-ModificarEmpresa.php?id_empresa=" + Cells(i, 2)
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.getElementById("txtStrPoblacion").Value = Cells(i, 3)
With IE.document
Set elems = .getElementsByTagName("input")
For Each e In elems
If (e.getAttribute("value") = "Save") Then
e.Click
Exit For
End If
Next e
End With
Next
MsgBox "Done"
End Sub
I am trying to work on something that clicks a button\line on a page, then clicks on others when the new page loads. I can get my vba to click the first button/link, but then when trying to click the next I am getting the error
Run-time error 91: Object variable or With block variable not set
Now I have to be honest, the code I do have is from research, and by no means do I understand it fully, I have searched all over trying to rectify the 2nd click, but nothing seems to be working.
the code I have so far is
Public Sub SHAREREPORTING()
Dim IE As SHDocVw.InternetExplorer
Dim Button As Object
Dim Button2 As Object
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.navigate "URL"
Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set Button = IE.Document.getElementById("b1_pki")
Button.Click
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "mypassword" , True
SendKeys "{ENTER}", True
Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set Button2 = IE.Document.getElementById("seite-itm-2")
Application.Wait (Now + TimeValue("0:00:05"))
Button2.Click
End Sub
the code for the 2 buttons ( I am calling them buttons/links, because I'm not too sure what they are, I don't understand javascript) I am trying to click at the moment is
Button1/Link1
<A onclick=loginViaPKI(); onkeypress=loginViaPKI(); id=b1_pki title="Log on with PKI - Button - To activate, press spacebar." class=urBtnEmph hideFocus style="WHITE-SPACE: nowrap" href="javascript:void(0);" ct="Button">Log on with PKI</A>
Button2/Link2
<TD onclick="return htmlbSL(this,7,'seite:sel_tab_2','','3','sel_tab_2')" id=seite-itm-2 class=urTbsLabelOff onkeydown="if (sapUrMapi_checkKey(event,'keydown',new Array('32'))){return htmlbSL(this,7,'seite:sel_tab_2','','3','sel_tab_2')};sapUrMapi_TabStrip_keySelect('seite',2,4,event);" noWrap><SPAN id=seite-itm-2-txt title="Bookmarks " class=urTbsTxtOff>Bookmarks </SPAN></TD>
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
I used the below code for loading the web site http://www.flashscore.com/soccer/england/premier-league/results/.
After I found and click on the "Show more matches" link, all the football matches are loaded in the browser.
The below code will give as results only the first half of matches, the events showed before pressing the "Show more matches" link.
My question is how can I list all the events URL adress?
Sub Test_Flashscore()
Dim URL As String
Dim ie As New InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim dictObj As Object: Set dictObj = CreateObject("Scripting.Dictionary")
Dim tRowID As String
URL = "http://www.flashscore.com/soccer/england/premier-league/results/"
With ie
.navigate URL
.Visible = True
Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set HTMLdoc = .document
End With
For Each objLink In ie.document.getElementsByTagName("a")
If Left(objLink.innerText, 4) = "Show" Or Left(objLink.innerText, 4) = "Arat" Then
MsgBox "The link was founded!"
objLink.Click
Exit For
End If
Next objLink
With HTMLdoc
Set tblSet = .getElementById("fs-results")
Set mTbl = tblSet.getElementsByTagName("tbody")(0)
Set tRows = mTbl.getElementsByTagName("tr")
With dictObj
'If if value is not yet in dictionary, store it.
For Each tRow In tRows
'Remove the first four (4) characters.
tRowID = Mid(tRow.ID, 5)
If Not .Exists(tRowID) Then
.add tRowID, Empty
End If
Next tRow
End With
End With
i = 14
For Each Key In dictObj
ActiveSheet.Cells(i, 2) = "http://www.flashscore.com/" & Key & "/#match-summary"
i = i + 1
Next Key
Set ie = Nothing
MsgBox "Process Completed"
End Sub
You need to wait a little while for the rest of the content to load - clicking the link fires off a GET request to the server, so that needs to return content and the content needs to be rendered on the page before you can grab it.
Clicking on that link takes you to fixtures. You can replace all that before dictionary with
.navigate "https://www.flashscore.com/football/england/premier-league/fixtures/"
That is:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer
With IE
.Visible = True
.navigate "https://www.flashscore.com/football/england/premier-league/fixtures/"
While .Busy Or .readyState < 4: DoEvents: Wend
'other code...using dictionary
'.Quit
End With
End Sub
I have this code where it goes to Minecraft.net and enters the username and password, and then goes to the profile page. I need to change the password on the account, which will be in cell A1 of the worksheet. I can't seem to figure out how to click the change password link. This is my code so far:
Dim IE As Object
Sub submitFeedback3()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://minecraft.net/login"
Application.StatusBar = "Submitting"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
' **********************************************************************
IE.Document.getElementById("username").Value = "dddddddd"
IE.Document.getElementById("password").Value = "ddd"
IE.Document.getElementById("signin").Click
'**********************************************************************
Application.StatusBar = "Form Submitted"
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
If the click doesn't work then you could try to submit the html form.
' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate "https://minecraft.net/login"
While IE.Busy
DoEvents
Wend
IE.Document.getElementById("username").Value = "dddddddd"
IE.Document.getElementById("password").Value = "ddd"
Dim htmlForm As HTMLFormElement
Set htmlForm = IE.Document.getElementById("loginForm")
htmlForm.submit
Within a page that I am able to open with VBA; I want to click on the first link in a table, then copy some data, close that window, click on the second link of the table, etc.
<a title="Open"
onmouseover="javascript:window.status='Click here!';return true;"
onmouseout="javascript:window.status='';return true;"
onclick="OpenWindow(this.href, 'Profile_5193622', 760, 565); return false;"
href="/Pages/Popups/Profile.aspx?pid=5193622">
I want to use the "/Pages/Popups/Profile.aspx?pid=5193622" in a URL and navigate to it. How do I need to go about this?
You have to click a few places to get there. This will take you there:
Sub ExtractFirstLeagueData()
Dim IE As Object, obj As Object
Dim League As Object
Dim links, link
Dim dict As Object
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.navigate ("http://whatifsports.com/hbd/Pages/Main/WorldRedirect.aspx?id=3")
WaitFor IE
IE.navigate ("http://whatifsports.com/HBD/Pages/World/PlayerSearch.aspx")
WaitFor IE
IE.document.getelementsbyname("ctl00$ctl00$ctl00$Main$PageOptionsPlaceHolder$PageOptionsPlaceHolder$lNameTextBox")(0).Value = "a"
IE.document.getelementsbyname("ctl00$ctl00$ctl00$Main$PageOptionsPlaceHolder$PageOptionsPlaceHolder$LevelDropDown$LevelDropDown")(0).Value = "5"
IE.document.forms(0).submit
WaitFor IE
Set dict = CreateObject("scripting.dictionary")
WaitFor IE
'collect the player links
Set links = IE.document.getElementsByTagName("a")
WaitFor IE
For Each link In links
If link.href Like "*/Popups/PlayerProfile.aspx?pid=*" Then
dict.Add link.innertext, link.href
End If
Next link
'navigate to each page and collect info
For Each link In dict.keys
IE.navigate dict(link)
WaitFor IE
Debug.Print IE.document.URL
'get player info here...
Next link
End Sub
Sub WaitFor(IE As Object)
While IE.readyState <> 4
DoEvents
Wend
End Sub
Dim links, link
Dim dict As Object
'-------------------------
'get to the right page...
'-------------------------
Set dict = CreateObject("scripting.dictionary")
'collect the player links
Set links = IE.document.getElementsByTagName("a")
For Each link In links
If link.href Like "*/Popups/PlayerProfile.aspx?pid=*" Then
dict.Add link.innerText, link.href
End If
Next link
'navigate to each page and collect info
For Each link In dict.keys
IE.navigate dict(link)
WaitFor IE
Debug.Print IE.document.URL
'get player info here...
Next link
Helper sub:
Sub WaitFor(IE As Object)
While IE.readyState <> 4
DoEvents
Wend
End Sub