VBA through Internet Explorer, Find String of Text and Use It - vba

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

Related

Issue with clicking on an img button by using alt in IE

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

Using VBA to click a link

I'm trying next to click a link to view information on a page using VBA to then move on to edit that information with VBA, But trying to figure out how to write the code for it as the ID information changes with each search,
Any ideas on this?
I've looked around and can't seem to understand how to get VBA to pick this line up as the (ID=) and it isn't joined to the same ID as I'm searching for.
There is also serval references for
This is the line of code.
View
This is my current code to do the search for it. Without clicking on the view section yet.
Sub Test()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
With ie
.Visible = True
.navigate "*******"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("quicksearch").Value = var
'code to click the button
Set form = ie.document.getElementsByTagName("form")
Application.Wait (Now + TimeValue("0:00:02"))
Set button = form(0).onsubmit
form(0).submit
'wait for page to load
While ie.Busy
DoEvents
Wend
Next x
End Sub
Edited
I've added in the code I think it should be following that link and a bit of tinkering to get it to not error with compiler errors :D, All seems to work but when it get's to the line to click the link it doesn't fail but doesn't even click it. It will then move on to the next one in the list in the spreed sheet, Which is expected.
Following it through with the debugger that shows nothing erroring or failing which is what I expect it to do if the code was wrong or link,
Any help, please ?
This is the code now
Sub Test1()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
Dim a
Dim linkhref
linkhref = "/?do_Action=ViewEntity&Entity_ID"
With ie
.Visible = True
.navigate "*******"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("quicksearchbox").Value = var
'code to click the button
Set form = ie.document.getElementsByTagName("form")
Application.Wait (Now + TimeValue("0:00:02"))
Set button = form(0).onsubmit
form(0).submit
'wait for page to load
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
For Each a In ie.document.getElementsByTagName("a")
If (a.getAttribute("href")) = ("/?do_Action=ViewEntity&Entity_ID=") Then
a.Click
Exit For
Application.Wait (Now + TimeValue("0:00:02"))
While ie.Busy
DoEvents
Wend
End If
Next
Next x
End Sub
This is a copy of the code around the buttons,
This is the code surrounding the buttons,
View
Decom
Log</td>
Thank you.
Use a For to check every <a> tag element and make sure you click the right one. It's true your ID changes, but rest of string is constant, so that's 1 factor. Also, it looks like you always will click where it says View so that's another constant.
With both options, we can develop a simple For..Next that will check every <a> element and will check if those 2 options requirements are fulfilled:
For Each a In ie.document.getElementsByTagName("a")
If Left(a.href, 37) = "/?do_Action=ViewEntity&Entity_ID=" And a.innerText = "View" Then
a.Click
Exit For
Next a
Try it and let's see if this works for you.
If the element href is something like "/?do_Action=ViewEntity&Entity_ID=14287", this if statement is never going to evaluate to True:
(a.getAttribute("href")) = ("/?do_Action=ViewEntity&Entity_ID=")
So the element will never be clicked.
If you know the Entity_ID you want to click you can do:
Dim Entity_ID as Integer
Entity_ID = 14287
If (a.getAttribute("href")) = "/?do_Action=ViewEntity&Entity_ID=" & Cstr(myID) Then a.click
Otherwise just check if the element href contains that url:
If InStr(1, a.getAttribute("href"), linkhref) > 0 Then a.click
EDIT
Ok, using the HTML you posted I am able to access the specified a tag that you requested, by doing this
For Each ele In ie.document.getElementById("searchresults").getElementsByTagName("a")
If InStr(1, ele.href, "do_Action=ViewEntity") > 0 Then
MsgBox "The button is found!"
End If
Next

How to use wildcard in excel vba internet explorer

could you please tell me how to use wildcards in excel vba internet explorer?
id="btn_edit_card_1NLQNQD0D93O"
everytime that number in different. How to click that button via
document.getElementById
There is no class in code
<a id="btn_edit_card_1NLQNQD0D93O" href="/trades/bejelentes_egyszerusitett/1NLQNQD0D93O">Editing basic data</a>
As #Nathan_Sav suggested, very wisely I might add, that you will have to work around your problem with collections. For example, loop through all the "a" tags until you find one that "btn_edit_card_" as part of it's name. This will work unless there are more than one "a" tags with that phrase in it.
Set els = ie.Document.getElementsByTagName("a")
For Each el In els
If el.ID Like "btn_edit_card*"
el.click
Exit For
' Debug.Print el.ID, el.Name
End If
Next el
If your IE version is 9 or above you can use the querySelectorAll method of the HTMLDocument class.
This uses CSS selectors to enable filtering of elements by their attributes. In your case you are looking for a elements with an id beginning with btn_edit_card. The selector for this would be:
a[^=btn_edit_card]
Where the ^= means begins with.
See the example code below that pulls comments from this very page - they are all tr elements in a table beneath your question which all have id of comment-123456 where the number can change from comment to comment (because they are stored uniquely in the database etc):
Option Explicit
Sub GetElementByWildcard()
Dim objIe As InternetExplorer
Dim objDoc As HTMLDocument
Dim objElements As IHTMLDOMChildrenCollection
Dim objElement As Object
Dim lngCounter As Long
On Error GoTo ExitFunction
'get page content
Set objIe = New InternetExplorer
objIe.Visible = False
objIe.navigate "http://stackoverflow.com/questions/42225761/how-to-use-wildcard-in-excel-vba-internet-explorer"
Do While objIe.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'get document
Set objDoc = objIe.document
'get any <tr> with an id starting with comment-
Set objElements = objDoc.querySelectorAll("tr[id^=comment-]")
'iterate output
lngCounter = 0
While lngCounter < objElements.Length
Set objElement = objElements.Item(lngCounter)
Debug.Print objElement.innerText
lngCounter = lngCounter + 1
Wend
ExitFunction:
If Err.Number <> 0 Then
Debug.Print Err.Description
End If
objIe.Quit
Set objDoc = Nothing
Set objIe = Nothing
End Sub

Use VBA to list all URL address of a web page

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

Pasting URLs That Have Been Scraped From a Webpage

So I'm looking to dump a bunch of URLs from a webpage into excel as a list. I was previously dumping the items into a listbox, but I have found that listboxes are quite difficult to work with!
Once I have collected the URLs into a column in excel, I want excel to click on each link and find the email address that is on the page. Here is the coding that I currently have...
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
url_name = Sheet1.Range("A2")
If url_name = "" Then Exit Sub
IE.Navigate (url_name)
Do
DoEvents
Loop Until IE.ReadyState = 4
Set AllHyperLinks = IE.Document.GetElementsByTagName("A")
For Each hyper_link In AllHyperLinks
Range("x":"F").Value = hyper_link
This is all I have so far! I'm not sure how to complete the loop! I want the code to paste every new URL that it finds on the page in the next empty row in column F.
You can complete the loop in this way:
Dim IE As Object, LR As Long, i As Long
LR = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
For i = 2 To LR
url_name = Sheet1.Range("A" & i).Value
If url_name = "" Then Exit Sub
IE.Navigate (url_name)
Do
DoEvents
Loop Until IE.ReadyState = 4
Set AllHyperLinks = IE.Document.GetElementsByTagName("A")
For Each hyper_link In AllHyperLinks
Range("x":"F").Value = hyper_link
Next hyper_link
Next i
Please note that if you have large set of data, this is going to take a LOOOONGGGG time.