I am having a problem displaying some pictures (more than one) in a picturebox.
<div id="salary_total" style="display: block;"><table style="border: 3px solid rgb(71, 5, 6); padding-right: 1px;" bgcolor="#ffffff" cellpadding="0" cellspacing="0"><tbody><tr><td><img src="./images/counter/b.gif"></td>
<td><img src="./images/counter/3.gif" border="0"></td>
<td><img src="./images/counter/3.gif" border="0"></td>
<td><img src="./images/counter/0.gif" border="0"></td>
<td><img src="./images/counter/8.gif" border="0"></td>
</tr></tbody></table>
those picture links, shows like a number like 3308, and it changes every time the page loads. how can I display those pictures (next to each other) in a picturebox.
Try
Dim htmlDocument As HtmlDocument = Me.WebBrowser1.Document
Dim htmlElementCollection As HtmlElementCollection = htmlDocument.Images
For Each htmlElement As HtmlElement In htmlElementCollection
Dim imgUrl As String = htmlElement.GetAttribute("src")
If imgUrl.Contains("counter") Then
Me.PictureBox1.ImageLocation = imgUrl.Substring(0, 41)
End If
Next
This one works for the first picture, how can I have like 3 more pictureboxs, and do the same for the other 3 pictures?, like the 3.gif will go to the 1st picturebox, and so on?!
I figured it out: Here is the solution! Thanks
Try
Dim htmlDocument As HtmlDocument = Me.WebBrowser1.Document
Dim htmlElementCollection As HtmlElementCollection = htmlDocument.Images
Dim ImagesFound As Integer = 0
For Each htmlElement As HtmlElement In htmlElementCollection
Dim imgUrl As String = htmlElement.GetAttribute("src")
If imgUrl.Contains("counter") Then
ImagesFound+=1
Select Case ImagesFound
Case 1
PictureBox1.ImageLocation = imgUrl
Label1.Text = PictureBox1.ImageLocation.ToString()
Case 2
PictureBox2.ImageLocation = imgUrl
'... etc.
End Select
End If
Next
Catch ex As Exception
End Try
First, you ImageLocation will come out like this:
http://www.link.com./images/counter/8.gif
That's probably not what you intended.
Second, Shoban said that you should be using regular text and CSS. He's right.
Third, If you want to display multiple images in a single picturebox, you will need to make a single image object and draw the other images into it. There are VB.Net functions for that and also native Windows API (CopyRect?).
You can use multiple picture boxes if you like.
Related
I have a TextArea in a page of my Site, I want to display the text of this TextArea, using the Document of a WebBrowser Control, to a Label.
This is the TextArea definition:
<textarea class="cadr" id="bar" name="saisie"style="height: 260px; width: 700px;"> simple text</textarea>
How can I do it? I have this code:
WebBrowser1.Document.GetElementById(label1.text).GetAttribute("value", "bar")`
The Element you describe has no ID, so you have to resort to the className of both the TEXTAREA and its DIV parent (to narrow down the chance to get the wrong element).
<div class="labelapper"><textarea rows="3" cols="80" class="Label">text1</textarea></div>
You can use GetElementsByTagName() instead of GetElementsById(), then filter the results
Something like this:
Imports System.Linq
' [...]
dim innerText = String.Empty
dim textArea = WebBrowser1.Document.GetElementsByTagName("TEXTAREA").
OfType(Of HtmlElement)().
FirstOrDefault(Function(elm) elm.GetAttribute("className") = "Label" AndAlso
elm.Parent.GetAttribute("className") = "labelapper")
if textArea IsNot Nothing Then
innerText = textArea.InnerText
End If
EDIT: The HtmlElement in the original post has been changed, now showing an ID
dim textArea = WebBrowser1.Document.GetElementById("bar")
if textArea IsNot Nothing Then
label1.Text = textArea.InnerText
End If
I am connecting to a website trough a webbrowser, then i want to post a message on a message board.
This is the HTML of the text box:
<div class="fr-element fr-view" dir="ltr" contenteditable="true" style="min-height: 100px;" aria-disabled="false" spellcheck="true"><p>TEXT GOES HERE</p></div>
I have tried the following 2 codes:
For Each CurrentElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If CurrentElement.GetAttribute("class") = "fr-element fr-view" Then
CurrentElement.InnerText = TextBox1.Text
End If
Next
For Each CurrentElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If CurrentElement.GetAttribute("class") = "fr-element fr-view" Then
For Each InnerCurrentElement As HtmlElement In CurrentElement.GetElementsByTagName("p")
InnerCurrentElement.InnerText = RichTextBox1.Text
Next
End If
Next
None of them does anything
I really havent even messed around with HTML before this at all even so I am not sure what I could search for to solve my issue as I am able to handle other text boxes, for example the login ones which are like this:
<input type="text" class="input" name="login" autofocus="autofocus" autocomplete="username" id="aaaaa">
You must to do that after your WebBrowser have finished to load the document. A little example below:
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("https://html.com/tags/input/")
AddHandler WebBrowser1.DocumentCompleted, Sub(senderObj As Object, eObj As WebBrowserDocumentCompletedEventArgs)
Dim inputs As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
If inputs IsNot Nothing AndAlso inputs.Count > 0 Then
For Each current As HtmlElement In inputs
current.SetAttribute("value", "Eurekaaaaaaaaaaaaaaaaaaaaa")
Next
End If
End Sub
This is the original function I wrote to get the Html of a web page and parse it with the same code used for "IE.document"
The code works fine with some websites but now I get an error on "doc.write" and i think it's because the webpage has "iso-8859-1" encoding and a different encoding in the second column of the table I'm trying to parse.
Function mWebRe(ByVal mUrl As String) As MSHTML.HTMLDocument
Dim request As HttpWebRequest = WebRequest.Create(mUrl)
request.Timeout = 10000
Dim doc As MSHTML.IHTMLDocument2 = New MSHTML.HTMLDocument
Try
Dim response As HttpWebResponse = request.GetResponse()
'this is the original code
'Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
'this is an attempt without effects
Dim reader As StreamReader = New StreamReader(response.GetResponseStream(), Encoding.GetEncoding("iso-8859-1"))
Dim WebContent As String = reader.ReadToEnd() 'Here the text seems to be
doc.clear()
doc.write(WebContent) 'Here I get error on loading page
doc.close()
' The following is a must do, to make sure that the data is fully load.
While (doc.readyState <> "complete")
Thread.Sleep(50)
End While
Catch ex As Exception
Return Nothing
End Try
Return doc
End Function
I've tryed to modify the code and also tryed to use HtmlAgilityPack (I've never used it before) without success.
I need the content of the second "Table" (doesn't have id), so I wrote the code below (It isn't able to get the correct innertext from cells):
Dim web As HtmlAgilityPack.HtmlWeb = New HtmlWeb()
web.OverrideEncoding = Encoding.GetEncoding("ISO-8859-1")
Dim doc As HtmlAgilityPack.HtmlDocument = web.Load(mUrl)
For Each Table As HtmlNode In doc.DocumentNode.SelectNodes("//table")
For Each Row As HtmlNode In Table.SelectNodes("//tr")
For Each Cell As HtmlNode In Row.SelectNodes("//td")
Dim mTxt As String = Cell.InnerText
Next
Next
Next
This is the "start" of the webpage sourcecode:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
This is an extract of a row I would extract:
<tr>
<td class="tableValues" align="center" valign="top" >Mar 24/12/2013</td>
<td class="tableValues" align="left" valign="top" >Iscritto al Ruol<!--span-->o<!--i>4</i--></td>
<td class="tableValues" align="left" valign="top" ></td>
</tr>
I think that the second column has a different encoding but I don't have any idea on how to convert it to the correct text.
Any suggest is appreciated.
I just solved inserting the code below in the code with htmlAgilityPack.
But if anyone can suggest a better solution I'll be grateful.
For Each Cell As HtmlNode In Row.SelectNodes("//td")
Dim mTxt As String = Cell.InnerText
If mTxt.Contains("&#") Then
Dim StrOk As String = WebUtility.HtmlDecode(mTxt)
StrOk = Regex.Replace(StrOk, "<!--.+?-->", String.Empty)
Debug.Print(StrOk)
End If
I have a webbrowser in a form that I am trying to see, if the following two items being displayed or not
<div class="alert alert-danger bet" style="display: block;">You Lost</div>
<div class="alert alert-success bet" style="display: none;">You Won</div>
I have searched and I can not find a solution to this. I have found how to search for them and find them without them having an ID but i can not see the style. If possible i would like to put the style result in a textbox for now until i figure out how to deal with the outcome.
EDIT
Ok so here's where i am now
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("DIV")
For Each curElement As HtmlElement In theElementCollection
If curElement.OuterHtml.Contains("alert alert-danger bet") Then
TextBox1.Text = curElement.GetAttribute("style")
End If
Next
This returns System._ComObject and it seems to be the only thing that i can get to return at all.
Any suggestions?
OK so finally I believe I have a solution.
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("DIV")
For Each curElement As HtmlElement In theElementCollection
If curElement.OuterHtml.Contains("alert alert-danger bet") Then
If curElement.Style = "display: block;" Then
TextBox1.Text = "True"
ElseIf curElement.Style = "display: none;" Then
TextBox1.Text = "False"
End If
End If
Next
End If
Next
Now obviously this is only showing one of my items so I will still have to handle the other but at least this gives me a True or False statement.
The img src that I need to grab is:
div id="recaptcha_image" class="width: 300px; height: 57px;" style="width: 300px; height: 57px;">
<img width="300" height="57" src="http://www.google.com/recaptcha/api/image?c=03AHJ_VuvlvMA4JvVIQvDR4C_iDbOTwOF5FUIRPGkkSImDRYAD6sY2L0IxyJSpSP1WGjWqr0MQ-dmjkiIgevFY2gkMpNWi1cQbtgUZB5QaYr_vIHv6xFzG9ydFbBWs4xiEhWoxHEFUYHZj6CCh4obyZSOd2La0nozLZw" style="display:block;">
Here is my code that grabs all the img src tags from any website. The one thing is the img src that I need isn't listed in the returned results.
How can I correct my code to grab only this one field? Here is my working program...currently is doesn't load a pic into the picturebox...but it does return the results in the rich text box. Thanks
Dim s As String = TextBox1.Text
Dim hw As New HtmlWeb()
Dim doc As HtmlDocument = hw.Load(s)
Dim items As HtmlNodeCollection = doc.DocumentNode.SelectNodes("//img")
If items Is Nothing Then
MessageBox.Show("There is nothing to show you")
End If
If items IsNot Nothing Then
For Each item As HtmlNode In items
RichTextBox2.Text = RichTextBox2.Text & (item.GetAttributeValue("src", "value") & Environment.NewLine)
'PictureBox1.Load(item.GetAttributeValue("src", TextBox1.Text & "value"))
Next
Without the complete HTML it is difficult to say, but if you want the src for this specific img you can use the following in SelectNodes (which should probably be changed to SelectSingleNode).
doc.DocumentNode.SelectSingleNode("//div[#id='recaptcha_image']/img[1]").Attributes("src").Value
The above will return the src String for the img tag.