Get text in a div that doesn't have a name or id - html

I would like to get the text inside this code:
<div class="js-text-container"></div>
when there is an ID, i use getelementbyId, no problem, but in this case no ID and even nothing inside the 2 >< (although something is displayed)
I found an interesting solution here and tried to adapt it to my case:
Dim divs = WebBrowser1.Document.Body.GetElementsByTagName("div")
For Each d As HtmlElement In divs
If d.GetAttribute("class") = "js-text-container" Then
TextBox1.Text = d.InnerText
End If
Next
But nothing appears in my textbox. Do someone have an idea? I think its because InnerText refers to nothing in this case...
I hope I was clear enough.
Thanks a lot

Instead of d.GetAttribute("class") = "js-text-container"
use
d.GetAttribute("className") = "js-text-container"
I tested it locally, I believe you can use it in VB.Net
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("className") == "js-text-container")
{
textBox1.Text = el.InnerText;
}
Hope it helps!

Related

How to get text from this Html on webbrowser? [VB.NET]

I'm trying to parse this HTML code:
<textarea id="1" name="padText" class="col-xs-12 text-tab-1" style="min-height: 400px; height: 493px;">TEXT</textarea>
And I'm trying to use many codes.
This one is one of them :
Dim MailElement As HtmlElement = WebBrowser2.Document.GetElementById("1")
If MailElement IsNot Nothing Then 'Necessary check: Was the element found?
RichTextBox1.Text = MailElement.GetAttribute("value")
End If
End Sub
But unfortunately is not working.
I've tried to parse the textarea ID too, but it gives me back a simple "0".
Thanks
I found the solution by myself.
WebBrowser1.Document.All("padText").GetAttribute("value")

VBA Excel IE automation: locate element by custom tag

I need to pick out an element by a custom html tag - ie, where the custom tag would be "somecustomtag" in the following div element
<div class="panel-one" somecustomtag="blue">
I just can't remember the sytax. I know it's something like:
Set myElements = IE.Document.getElementsbyTagName("div")
For Each ob in myElements
If ob.subTag("somecustomtag") = "blue" then ' ????????
someStringVariable = ob.innerText
exit for
End If
Next ob
I've used this a dozen times before but can't find it any where. What is the proper syntax for .subTag?
In your case somecustomtag is an attribute. You will get the value of somecustomtag with the following code snippet
ob.getAttribute("somecustomtag")

How to change the font?

I want to change the font and set its style to bold.
I have two problems:
changing CharWeight works but not CharFontName
it applies "bold" to the whole paragraph, not only to the selection
Here's my code:
sub AddAnimation
xTextCursor = ThisComponent.CurrentController.Selection(0)
xText = xTextCursor.getText()
xText.CharFontName = "Consolas"
xText.CharWeight = com.sun.star.awt.FontWeight.BOLD
end Sub
Calling getText() gets the entire text, not just the selected part.
Sub ChangeFont
xTextCursor = ThisComponent.CurrentController.Selection(0)
xTextCursor.CharFontName = "Consolas"
xTextCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
End Sub
The font name changed when I tried it, using both LO and AOO.
Are you using CTL or CJK scripts? If so, then it needs to be CharFontNameComplex or CharFontNameAsian. However if CharWeight worked, then that must not be the issue.
One more guess: Maybe a style is overriding it.

VBS: Can I target a field by its tabIndex, and its div position?

Novice here, using VBS to help with data entry to a web input form. Would appreciate any advice. I regularly use lines like this to set the value of a field based on its name:
IE.Document.All.Item("field1").Value = "test"
However I have a set of very awkward fields whose names change with each record. Their physical positions stay the same (visually); their tabIndexes stay the same (1,2,3,4), so I wondered if it's possible to do something like this:
IE.Document.All.getElementByTabIndex(1).Value = "test"
...But I'm not sure it is? Furthermore, even if that did work, tabIndex1 is used for another field on the same webpage. The fields that I am interested in, however, are all located on a div. The ID of the div is "form_div". So I'm trying to target a field located on div "form_div" whose tabIndex is 1... do you think it is possible?
Big thanks in advance.
So you have a DIV element with tabIndex set to 1 and you don't know it Name or ID, right? Then do something like this:
Set oDivs = IE.Document.getElementsByTagName("div")
Set myDiv = Nothing
For Each od In oDivs
If od.tabIndex = "1" Then
Set myDiv = od
Exit For
End If
Next
If Not myDiv Is Nothing Then
'do what needs here...
MsgBox myDiv.Name
End If
P.S. Well, I see 2 drawbacks in your design.
The tabIndex should be unique.
Searching for element by name is not so perfect in IE. If your
element has only Name and not ID then getElementsByName will
fail. Better use ID, it's even simplify coding:
Set myDiv = IE.Document.All.form_div
To find it by Name w'd be:
Set oDivs = IE.Document.getElementsByTagName("div")
Set myDiv = Nothing
For Each od In oDivs
If od.Name = "form_div" Then
Set myDiv = od
Exit For
End If
Next
And once you have the element...
If Not myDiv Is Nothing Then
Set nodes = myDiv.childNodes
For i = 0 To nodes.Length-1 Step 2
If nodes(i).tabIndex = "1" Then
'do what need here...
nodes(i).Value = nodes(i).tabIndex
Exit For
End If
Next
End If

replace keyword within html string

I am looking for a way to replace keywords within a html string with a variable. At the moment i am using the following example.
returnString = Replace(message, "[CustomerName]", customerName, CompareMethod.Text)
The above will work fine if the html block is spread fully across the keyword.
eg.
<b>[CustomerName]</b>
However if the formatting of the keyword is split throughout the word, the string is not found and thus not replaced.
e.g.
<b>[Customer</b>Name]
The formatting of the string is out of my control and isn't foolproof. With this in mind what is the best approach to find a keyword within a html string?
Try using Regex expression. Create your expressions here, I used this and it works well.
http://regex-test.com/validate/javascript/js_match
Use the text property instead of innerHTML if you're using javascript to access the content. That should remove all tags from the content, you give back a clean text representation of the customer's name.
For example, if the content looks like this:
<div id="name">
<b>[Customer</b>Name]
</div>
Then accessing it's text property gives:
var name = document.getElementById("name").text;
// sets name to "[CustomerName]" without the tags
which should be easy to process. Do a regex search now if you need to.
Edit: Since you're doing this processing on the server-side, process the XML recursively and collect the text element's of each node. Since I'm not big on VB.Net, here's some pseudocode:
getNodeText(node) {
text = ""
for each node.children as child {
if child.type == TextNode {
text += child.text
}
else {
text += getNodeText(child);
}
}
return text
}
myXml = xml.load(<html>);
print getNodeText(myXml);
And then replace or whatever there is to be done!
I have found what I believe is a solution to this issue. Well in my scenario it is working.
The html input has been tweaked to place each custom field or keyword within a div with a set id. I have looped through all of the elements within the html string using mshtml and have set the inner text to the correct value when a match is found.
e.g.
Function ReplaceDetails(ByVal message As String, ByVal customerName As String) As String
Dim returnString As String = String.Empty
Dim doc As IHTMLDocument2 = New HTMLDocument
doc.write(message)
doc.close()
For Each el As IHTMLElement In doc.body.all
If (el.id = "Date") Then
el.innerText = Now.ToShortDateString
End If
If (el.id = "CustomerName") Then
el.innerText = customerName
End If
Next
returnString = doc.body.innerHTML
return returnString
Thanks for all of the input. I'm glad to have a solution to the problem.