Using this HTML code for example:
<table class="table-grid">
<tr>
<th>auto.model</th>
<td>
<pre>'Toyota Avensis Wagon'</pre>
</td>
</tr>
<tr>
<th>auto.year</th>
<td>
<pre>2005</pre>
</td>
</tr>
</table>
If I take the parameter "auto.model" between <th></th> tags and want to receive "Toyota Avensis Wagon", i.e. the next expression between <pre></pre>. Ideally I'd like to have function to do it.
Thank you #Jeeped, but code raise "Type mismatch" error and points to Set el = Param.PreviousSibling:
Sub Extract_TD_text()
Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim Params As IHTMLElementCollection
Dim Param As HTMLTableCell
Dim Val As HTMLTableCell
Dim r As Long
Dim el As HTMLTableCell
URL = "My URL"
Set IE = New InternetExplorer
With IE
.navigate URL
.Visible = False
'Wait for page to load
While .Busy Or .READYSTATE <> READYSTATE_COMPLETE: DoEvents: Wend
Set HTMLdoc = .document
End With
Set Params = HTMLdoc.getElementsByTagName("tr")
For Each Param In Params
If Param.innerText Like "*auto.model*" Then
Set el = Param.PreviousSibling
Exit For
End If
Next
If Not el Is Nothing Then Debug.Print el.innerText
IE.Quit
Set IE = Nothing
End Sub
Instead of using previousSibling, I'd like to suggest nextElementSibling.
From the way your HTML and VBA codes are currently set up, the current 'param' value being passed should be the <th> tag. I think previousSibling would likely check the tag that comes before that, and since is the first element within the <tr> (the parent element), there shouldn't be anything (except maybe an invisible node- which previousSibling can find, but that we don't need).
I think nextElementSibling should be able to find your <td> tag, since it comes after your <th> tag.
Related
I'm creating a code that allows me to open a specific site and enter value in element with the name searchByTagName=searchByTRSMP and then Type search to load the new window
But the problem that button search doesn't have a TagName or IdName only this
<td width="13%" align="center" rowSpan="1" colSpan="1">
<input name="" onclick="Javascript:document.forms[0].submit();return false;" type="image" srx="/cmh/cmh/xxxxxx" border="0"></input>
Anyone Can Light me on pressing that button with only those conditions
this Mycode :
Sub ToComb()
Dim ie As Object
Dim itm As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://XXXX-
XXXX.eu.airbus.XXXXXp:XXXXX/cmh/consultation/preSearchTRSTDMAS.do?
clearBackList=true&CMH_NO_STORING_fromMenu=true"
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set itm = ie.document.getElementsByName("searchByTRSMP")(0)
If Not itm Is Nothing Then itm.Value = "k20734"
Set Doc = ie.document
Set tags = ie.document.getElementsByTagName("")
For Each tagx In tags
If tagx.Value = "Next" Then
tagx.Click
Exit For
End If
Next
End Sub
GetElementsByTagname mean search for an element by the type of an HTML element (such as, div, p or in your example - input).
You can get all your inputs tags (elements), iterate them and identify the required input, based on it's (for example) srx attribute:
Set tags = ie.Document.GetElementsByTagname("Input")
For Each tagx In tags
If tagx.src= "/cmh/cmh/xxxxxx" Then
tagx.Click
End If
Next
In addition, the final src of the input might changed from the actual code, because you use a relative path. Check the actual src with a MsgBox:
For Each tagx In tags
MsgBox tagx.src
I assume it will be different, such as prefix of http and so on:
If tagx.src = "http://xxxx
xxxx.eu.airbus.xxx:xxxx/cxxx/xxx/image/button_search.gif" Then
I have a HTML table something like this:
<table class="Example">
<tr>
<th>Header1</th>
<td>Value1></td>
</tr>
<tr>
<th>Header2</th>
<td>Value2></td>
</tr>
</table>
I want to find the <th> equal to "Header2" and then return the corresponding <td>, i.e. "Value2", which is inside the same <tr> .
I know I can easily use the index number, e.g. getElementsByTagName("td")(1) to find this value, but this is not feasible since each page may have the rows jumbled up.
I've tried doing this varying ways with no success. Hopefully, the following code indicates what I'm trying to do:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Sub WebSearch()
Dim URL As String
Dim IE As Object
Dim HWNDSrc As Long
Dim html As IHTMLDocument
Dim Example As IHTMLElement
Dim TableRows As IHTMLElementCollection
Dim TableRow As IHTMLElement
Dim RowChildren As IHTMLElementCollection
Dim RowChild As IHTMLElement
Dim TableHeader As IHTMLElement
Dim TableData As IHTMLElement
URL = "https://..."
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate URL
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
HWNDSrc = IE.HWND
SetForegroundWindow HWNDSrc
Set html = IE.document
On Error Resume Next
Set Example = html.getElementsByClassName("Example")(0)
'''''''' Trying to get Result
Set TableRows = Example.Children
For Each TableRow In TableRows
Set RowChildren = TableRow.Children
For Each RowChild In RowChildren
Set TableHeader = RowChild.getElementsByTagName("th")(0)
Set TableData = TableHeader.NextSibling
If TableHeader.innerText = "Header2" Then MsgBox TableData.innerText
Next
Next
IE.Quit
Set IE = Nothing
Application.StatusBar = ""
End Sub
I want to extract the projectstatus of a project which I can find on a website. See below for an example how the html is parsed. I want to extract the text Start which is the text between td and /td. See below the html my code.
<div id="ProjectStatus">
<tr>
<th>
<span id="ProjectStatus_Label1" title="De status van het project">Projectstatus</span>
</th>
<td>Start</td>
</tr>
Below you'll find the code that I have at this moment. This code only gives me the string "Projectstatus", which is not what I want. How can I extract the word "Start"?
Private Sub btnClick()
Dim ieApp As InternetExplorer
Set ieApp = New InternetExplorer
Set ieApp = CreateObject("internetexplorer.application")
With ieApp
.Navigate "url"
.Visible = True
End With
Do While ieApp.Busy
DoEvents
Loop
Set getStatus = ieApp.Document.getElementById("ProjectStatus_Label1")
strStatus = getStatus.innerText
MsgBox (strStatus) 'gives met the text "Projectstatus, but I need the text "Start"
ieApp.Quit
Set ieApp = Nothing
End Sub
Achieving this, starting from the ProjectStatus_Label1, will require some DOM navigation.
Use the following:
Do While ieApp.Busy
DoEvents
Loop
Set labelSpan = ieApp.Document.getElementById("ProjectStatus_Label1")
Set tableHeader = labelSpan.Parent
Set tableRow = tableHeader.Parent
For Each child In tableRow.Children
If child.tagName = "TD" 'This is the element you're looking for
Debug.Print child.innerText
Exit For
End If
Next
Of course, I highly recommend you revise this code and use explicit declarations and Option Explicit, but you haven't in your question so I won't in my answer.
Also, I've used a number of assignments (labelSpan, tableHeader) for demonstrative purposes. You can use Set tableRow = ieApp.Document.getElementById("ProjectStatus_Label1").Parent.Parent and remove those other declarations.
Or you can use the code-golfy, harder-to-understand approach, starting from the ProjectStatus div:
Debug.Print ieApp.Document.getElementById("ProjectStatus").GetElementsByTagName("td")(0).innerText
I'm trying to click 'a' element on a webpage and I can't find out why it does not work.
Here is my VBA code.
Function answer1(ie3 As InternetExplorer, str_anwer As String, answerid As String)
Dim ie4 As New InternetExplorer
Dim a As Object
Set ie4 = ie3
ie4.Document.getElementbyId("view=" & answerid).Click
ie4.Document.getElementbyId("reply_cont").Value = str_anwer
End Function
Error: Property not found
Here's the HTML code from the webpage I think it is located in
<tr>
<td class="thm">208975260</td>
<td><pre>교환</pre></td>
<td class="subject">작동이안되서 교환 원합니다 어떻게 하면되나요?</td>
<td class="id"><span class="thm">st******</span><br>한혜진</td>
<td class="thm">2016.09.29 12:53:57</td>
<td id="date208975260"><span class="point2 ls1">미답변</span>
</td>
<td class="ansr">-</td>
</tr>
Sorry for my English
I'm not fluent English.
Please, let me know why it is not working
Without reference to Microsoft Internet Controls (SHDocVw) and Microsoft HTML Object Library the code could look something like the following. Note the IsNull call. When getElementbyId is called like this and the element is not found on the page this function returns Variant\Null.
In commented code a second example is shown. In this case the references were added and getElementbyId was called on variable of type HTMLDocument. Here in case the element was not found on the page this function returns Nothing.
Sub main()
Dim ie, url, readyStateComplete
readyStateComplete = 4
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
url = "your-url"
ie.navigate url
While ie.Busy Or ie.readyState <> readyStateComplete: DoEvents: Wend
answer1 ie, "<anwer>", "208975260"
ie.Quit
End Sub
Function answer1(ie As Variant, str_anwer As String, answerid As String)
Dim a As Object
If Not IsNull(ie.Document.getElementbyId("view" & answerid)) Then
ie.Document.getElementbyId("view" & answerid).Click
End If
If Not IsNull(ie.Document.getElementbyId("reply_cont")) Then
ie.Document.getElementbyId("reply_cont").Value = str_anwer
End If
' Dim htmlDoc As HTMLDocument
' Set htmlDoc = ie.document
' If Not htmlDoc.getElementbyId("reply_cont") Is Nothing Then
' htmlDoc.getElementbyId("reply_cont").Value = str_anwer
' End If
End Function
Read about difference between Early/Late Binding.
I'm trying to make an Excel macro that inserts data into a web search form and then copies the results into a table. The web search form is not actually a "form", but a blank table so I can't just change the input value of the form because there is none:
<td valign="top">
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<th class="navLabelText" align="left">Order:</th>
<td>
<input class="navEditField" id="opt_ordernumber_int" name="ordernumber" type="text" size="6" maxlength="6" />
</td>
</tr>
</table>
</td>
<td width="10"> </td>
The HTML just continues with more of the same types of forms (I'm guessing coded in Java since the site is a .jsp). Is there any way that I can pass values into the blank table?
Here's what I have so far:
Sub featurecode()
Dim ie As Object
Dim doc As HTMLDocument
Dim links As IHTMLElementCollection
Dim link As HTMLAnchorElement
Dim i As Integer
Dim found As Boolean
Dim todaysURL As String
Dim objElement As Object
Dim objCollection As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True 'false
ie.navigate "https://website.com"
Application.StatusBar = "Loading Feature Codes"
Do Until ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
Set doc = ie.document
' Find the input tag of the order form and submit button:
Set objCollection = ie.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "ordernumber" Then
' Set text for search
objCollection(i).Value = "655032"
Else
If objCollection(i).Type = "submit" And objCollection(i).Name = "Submit" Then
' "Search" button is found
Set objElement = objCollection(i)
objElement.Click
End If
End If
i = i + 1
Wend
End Sub
The part that I'm having trouble with is this:
If objCollection(i).Name = "ordernumber" Then
' Set text for search
objCollection(i).Value = "655032"
Usually you can change the HTML value of the form, but in this case there is no HTML value in the input tag, so I'm at a loss. My goal here is to simply insert an order number into the form and hit the submit button. Unfortunately I can't show you the website as it's an internal corporate site, but here's a screenshot of the relevant info: screenshot
Thanks!
I've found with some elements in VBA, including INPUT, you have to focus on the element first:
objCollection(i).Focus
objCollection(i).Value = "655032"