write text inside div element use webbrowser vb.net - vb.net

Need to write in element .
This is the HTML code
< div id="t" class="message-tools__textarea js-scroller" contenteditable="true" data-placeholder="Write a message (Enter to send)">XXXXXX< /div>
My question is how to write text like XXXXXX ?????

Add this:
<div id="t" runat="server" ...
And then in your codebehind:
t.InnerText = "XXXXXXX" ' or t.InnerHTML if you're adding HTML code).

So, if the text to be replaced is fixed and has only 1 instance within the HTML code, it should be rather simple:
Dim OrigCode, ModifiedCode as string
OrigCode = GetGoogleCodeFromURL ' get the code
ModifiedCode = OrigCode.Replace("XXXXXXX","ZZZZZZ")
Dim MyHTML as string = "<head>...</head><body><h1>Hello World!</h1><p> </p>" & _
ModifiedCode & "<p> </p><p>That's it.</p>"
But usually, things are a bit more complicated, so I'm not sure, if you expressed your wish precisely. Also, if the code is big and action is recursive, it might be better to break it into parts and handle only relevant part of it, due to performance issues.

Related

Why HtmlAgilityPack adds some characters to my html

Here is my code:
Dim input = "<div><textarea>something</div></textarea>"
Dim doc As New HtmlAgilityPack.HtmlDocument
doc.OptionOutputAsXml = True
doc.LoadHtml(Input)
Using writer As New StringWriter
doc.Save(writer)
Dim res = writer.ToString
End Using
and the value of 'res' is:
"<?xml version="1.0" encoding="windows-1255"?>
<div>
<textarea>
//<![CDATA[
something
//]]>//
</textarea>
</div>"
the result as html is: My textarea
How can I prevent it ?
From my understanding of it, the reason is implied by this answer to Set textarea value with HtmlAgilityPack:
A <textarea> element doesn't have a value attribute. It's content is it's own text node:
<textarea>
Some content
</textarea>
To simulate the same thing safely, HAP has to enclose the content in a //<![CDATA[ section.
The source code for HAP has this comment for the relevant line(s):
// tags whose content may be anything
ElementsFlags.Add("textarea", HtmlElementFlag.CData);
So, you can't prevent it.

strip javascript from string but keep CSS styles

I generate some HTML dynamically and I need to make sure that any type of malicious javascript doesn't make it into the HTML. So I have this code to "clean" the input before I save it to the database:
Input = Input.Replace("'", "\'")
Input = Input.Replace(Chr(34), "\" & Chr(34))
Input = Input.Replace(vbCrLf, "")
Return System.Web.HttpUtility.HtmlEncode(Input)
This does clean the input, but it is also stripping any inline CSS that I have inside the input. Is there a way to strip out potentially malicious code yet keep the CSS tags?
Thanks!
you can use this line
Private input = Regex.Replace(input, "<script.*?<\/script>|<script.*?\/>", "")

Uploading Files Not Working - trim the initial characters

I am trying to upload a file using the WebBrowser control. It trims the starting character sometimes one and sometimes three then choose window gives error Invalid file name!. Can't seem to do it and need some help.
Here is the Html:
<input name="UploadedFile" id="UploadedFile" type="file" />
<input name="up" id="up" type="button" value="Upload" />
Here is the vb code:
Dim el = elc.GetElementsByName("UploadedFile")
el.Item("UploadedFile").Focus()
' SendKeys.Send("Capture.png" & "{ENTER}")
SendKeys.Send("C:\Capture.png" + "{ENTER}")
el.Item("UploadedFile").InvokeMember("Click")
that the file upload button comes up and hit enter, but can't input full filename into the file name area.
If I use thisSendKeys.Send("C:\Capture.png" + "{ENTER}"). It gives this error:
Choose window error screenshot
If I use this SendKeys.Send("Capture.png" + "{ENTER}"). It gives this error:
Choose window error screenshot
And if I put extra character then it works fine but it doesn't always trim one character so I can't put an extra character to solve this error.
The problem might be that the sendkeys comes up too fast behind focus, so the first few characters don't get picked up. Try just filling the textbox all at once with one line by setting the textbox's value rather than trying to imitate user keystrokes, this could replace both the focus and sendkeys lines:
WebBrowser1.Document.GetElementById("UploadedFile").SetAttribute("value", "C:\Capture.png")
...and then call the button-click
You are right #soohoonigan the sendkeys comes up too fast but that is not an answer for that. I did this like that.
Here is my code:
Dim el = elc.GetElementsByName("UploadedFile")
SetFile()
el.Item("UploadedFile").InvokeMember("Click")
Public Async Sub SetFile()
Await Task.Delay(1000)
SendKeys.Send("c:\Capture.png" & "{ENTER}")
End Sub
It's working fine.

Getting Visual Basic Program to Type into an HTML <p> and <input> tag with no id/name

So I have a program that takes a few inputs from textboxes and then adds them all up into two strings. Basically here is the problem, I am trying to get one of the strings to go into the forum post "title" and one into the forum "body". (This is not a spam program, it makes it easier for people to post ban reports for players on our server.) Here is the HTML codes for the website I am trying to type into;
<p class="ipsField_content">
<input id="topic_title" class="input_text" type="text" size="60" maxlength="150" name="TopicTitle" value="" tabindex="2">
</p>
The HTML code above is for the topic title area I want to type into. The problem with this is that it always says how it can not find this area.
'This is the HTML code for the forum body:
<body spellcheck="true" class="cke_show_borders">
<p>This is where I want to be able to type</p>
</body>`
With the HTML code above, I can't seem to get this VB code below to select that text area and enter in my string.
WebBrowser1.Document.GetElementsByTagName("p").InnerText = post
One reason is because VB doesn't allow me to use .innertext on a tag, and the other is that I do not know how to really specify which "p" tag. Like there is multiple "p" tags on this webpage, but it is the only "p" inside the "body" tag, if I could specify that somehow.
Here is the VB code I am using currently for the program.
Dim Report As String
Report = YourName & Suspect2 & Server & Time2 & Reason & RulesBroken & Proof
Dim topictitle As String
topictitle = ("" & Suspect & " - " & txtReason.Text & txtReasonCustom.Text & "")
txtTitle.Text = topictitle
txtPost.Text = Report`
making sure that the words "sign out" is on the page to insure the webbrowser is signed in.
`Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
If WebBrowser1.DocumentText.Contains("Sign Out") Then
WebBrowser1.Navigate("www.ThisLinksToMakeNewPostOnTheWebsite.com")
WebBrowser1.Document.GetElementById("topic_title").InnerText = txtTitle.Text
WebBrowser1.Document.GetElementByTagName("body").firstchild.InnerText = txtPost.Text
End If`
this code below is used to click the submit button
`WebBrowser1.Document.Forms(0).InvokeMember("submit")`
also this is the html code for the "Post New Topic" button
<input type="submit" name="dosubmit" value="Post New Topic" tabindex="50" class="input_submit" accesskey="s">
Anyways that is it. I tried to include as much information and as much lines of code as I could to show that I really am trying.
Please help, I have tried the MSDN links, but I can't seem to apply it to this.
From your code I think you are using the WebBrowser control. There is a much less resource hogging alternative. Since you are really only interested in posting to a form, you don't need the overhead of a browser. You can use the HTTPWebRequest to POST the data/text from your textboxes directly to a URL. An example can be found here:
http://howtostartprogramming.com/vb-net/vb-net-tutorial-51-httpwebrequest-post-method/
It's not the best tutorial in the world but it will get you on your way. You would of course still need to know what data you need to send to the web site.

How to get elements inside of commented out code HtmlAgilityPack in VB.NET

Is there a way to use HtmlAgilityPack on html that is inside <!-- --> comment blocks? For example, how can I target the inner text of "//div.[#class='theClass']" that is inside a block like this:
<!-- <div class="theClass'>Hello I am <span class="theSpan">some text.</span> </div>-->
So that I get
Hello I am some text.
The reason I ask is because I kept finding that this kept returning NULL, because the div's are inside comments:
htmlnodes = htmldoc.DocumentNode.SelectNodes("//div[#class='theClass']")
Unfortunately, XPath treats comment node content as plain text, means you can't query the content just like common nodes.
One possible way is to parse the comment node content as another HtmlDocument so you can query from it, for example :
'get desired comment node'
Dim htmlnode As HtmlNode = htmldoc.DocumentNode.SelectSingleNode("//comment()[contains(., theClass)]")
Dim comment As New HtmlDocument()
'remove the outer <!-- --> so we have clean content'
comment.LoadHtml(htmlnode.InnerHtml.Replace("<!--", "").Replace("-->", ""))
'here you can use common XPath query again'
Dim result As HtmlNode = comment.DocumentNode.SelectSingleNode("//div[#class='theClass']")
'following line will print "Hello I am some text."'
Console.WriteLine(result.InnerText)