Remove Javascript/ChildElement from WebBrowser Control - c#

I am using below code to inject javascript
HtmlElement head = _wb.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = _wb.Document.CreateElement("script");
mshtml.IHTMLScriptElement element = (mshtml.IHTMLScriptElement)scriptEl.DomElement;
element.text = "function zoom(){document.body.style.zoom='150%';}";
head.AppendChild(scriptEl);
Now can anyone tell me how to remove the added child

I think explicit deleting of an element is not possible (did not check the IHTML interfaces on this).
But this can be done in 2 different ways too:
element.OutherHtml = string.empty; => removes the whole element but does not always work
HtmlElement blankScript = _wb.Document.CreateElement("script");
element = blankScript; => replaces your unwanted script with a blank one

Related

C# Click on HtmlElement With no ID only Class

I am not using selenium nor anything else, i just want to do it on the webbrowser on the windows form application.
I have a windows form application and i want to click on a button with code but there is no ID.
I tried using a lot of different things found on this websites forums, but none of this works.
Have you tried using WebBrowser.GetElementByTagName("div") and then checking each element against attribute type=submit?
Your code should look something like
HtmlElement submit = FindSubmitElement(webBrowser1.Document);
submit?.InvokeMember("submit");
public HtmlElement FindSubmitElement(HtmlDocument document)
{
HtmlElementCollection elems = document.GetElementsByTagName("div"); // since your tag is div
// this will return collection, even in case there is just one div, find the first one, having an attribute 'type' with value 'submit'
foreach (HtmlElement elem in elems)
{
string type = elem.GetAttribute("type");
if (!string.IsNullOrEmpty(type) && type == "submit")
{
return elem; // if div tag with attribute type is found exit and return that html element
}
}
return null; // if no div tags found with an attribute 'type' return null
}
Check more on GetElementsByTagName method on the MSDN docs. Code is taken from there and adjusted to your need.

dotnetbrowser c# help remove entire div

I am using TeamDev Dotnetbrowser, for a tests, so how can I remove entire div, without a id, just using classname, for example:
<div class="home modulo-testbar">test</p></div>
I hope someone could help me, please!
Regards.
Learning documentation, I found a way to "hide", but I still like remove entire div content.
I have created and used this code:
DOMDocument document = e.Browser.GetDocument();
List<DOMNode> divs = document.GetElementsByTagName("div");
foreach (DOMNode node in divs)
{
DOMElement element = node as DOMElement;
if (element.GetAttribute("class").ToLower().Contains("home modulo-testbar"))
{
element.SetAttribute("style","display:none");
}
}
From looking at DOMNode API docs, this seems like it should remove a node from its parent:
var parent = node.Parent;
if(parent != null) {
parent.removeChild(node);
}
Put the above code where you currently set the style attribute to hide the element.

disable "this document has been modified"-message

I am changing the content on my webbrowser control using 2 different methods. I am only able to disable the message in one situation.
method 1:
I just need to add some text at the bottom of the page (i don't get the message)
string s = browser.DocumentText + "<a>Extra Text</a>";
browser.Document.Write(string.Empty);
browser.DocumentText=s;
method 2:
When i try to create a new element and add it to the webbrowser I still get the "this document has been modified"-message. Can i disable this message?
HtmlElement element = browser.Document.GetElementById("myId");
HtmlElement newElement = browser.Document.CreateElement("a");
newElement.InnerText = "Extra text";
element.AppendChild(newElement);
Try
myWebBrowser.Document.ExecCommand("Refresh", False, "")

C# Navigate to Anchors in WebBrowser control

We have a web browser in our Winforms app to nicely display history of a selected item rendered by xslt.
The xslt is writing out <a> tags in the outputted html to allow the webBrowser control to navigate to the selected history entry.
As we are not 'navigating' to the html in the strict web sense, rather setting the html by the DocumentText, I can't 'navigate' to desired anchors with a #AnchorName, as the webBrowser's Url is null (edit: actually on completion it is about:blank).
How can I dynamically navigate to Anchor tags in the html of the Web Browser control in this case?
EDIT:
Thanks sdolphion for the tip, this is the eventual code I used
void _history_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
_completed = true;
if (!string.IsNullOrEmpty(_requestedAnchor))
{
JumpToRequestedAnchor();
return;
}
}
private void JumpToRequestedAnchor()
{
HtmlElementCollection elements = _history.Document.GetElementsByTagName("A");
foreach (HtmlElement element in elements)
{
if (element.GetAttribute("Name") == _requestedAnchor)
{
element.ScrollIntoView(true);
return;
}
}
}
I am sure someone has a better way of doing this but here is what I used to accomplish this task.
HtmlElementCollection elements = this.webBrowser.Document.Body.All;
foreach(HtmlElement element in elements){
string nameAttribute = element.GetAttribute("Name");
if(!string.IsNullOrEmpty(nameAttribute) && nameAttribute == section){
element.ScrollIntoView(true);
break;
}
}
I know this question is old and has a great answer, but this hasn't been suggested yet, so it might be useful for others that come here looking for an answer.
Another way to do it is use the element id in the HTML.
<p id="section1">This is a test section</p>
Then you can use
HtmlElement sectionAnchor = webBrowserPreview.Document.GetElementById("section1");
if (sectionAnchor != null)
{
sectionAnchor.ScrollIntoView(true);
}
where webBrowserPreview is your WebBrowser control.
Alternatively, sectionAnchor.ScrollIntoView(false) will only bring the element on screen instead of aligning it with the top of the page

How to retrieve the scrollbar position of the webbrowser control in .NET

I tried using webBrowser1.Document.Body.ScrollTop and webBrowser1.Document.Body.ScrollLeft, but they don't work. They always return 0 and I can't access webBrowser1.Document.documentElement.ScrollTop and .ScrollLeft.
OK, I solved it:
Dim htmlDoc As HtmlDocument = wb.Document
Dim scrollTop As Integer = htmlDoc.GetElementsByTagName("HTML")(0).ScrollTop
To actually scroll, we found that the ScrollIntoView method worked nicely. For example, to scroll to the top-left of the page.
this.webBrowser.Document.Body.FirstChild.ScrollIntoView(true);
However, we were not successful in actually getting the scroll position (that said, we didn't spend long trying). If you are in control of the HTML content, you might consider using some javascript to copy the scroll position into a hidden element and then read that value out using the DOM.
ScrollTop and ScrollLeft merely allow an offset to be provided between the boundary of an element and its content. There appears to be no way to manipulate the scroll by those values. Instead, you have to use ScrollIntoView.
For anyone interested, here's the C# code equivalent to Marc's answer:
System.Windows.Forms.HtmlDocument htmlDoc = webBrowser.Document;
if (htmlDoc != null)
{
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
int scrollLeft = htmlDoc.GetElementsByTagName("HTML")[0].ScrollLeft;
}
I was able to query the scroll position using this
if (this.webBrowser.Document != null)
{
int scrollPosition = this webBrowser.Document.Body.ScrollTop;
}
You can check documentElement
IHTMLElement2 page =
(wb.Document.DomDocument as IHTMLDocument3).documentElement as IHTMLElement2;
int pos = page.scrollTop;
Accepted answer is VB. For C# WPF WebBrowser, I had to write the following. No idea if I really need all those casts or not. If one can get rid of any of those casts, that would be terrific.
using mshtml;
int? GetScrollTop(System.Windows.Controls.WebBrowser browser)
{
object doc = browser.Document;
HTMLDocument castDoc = doc as HTMLDocument;
IHTMLElementCollection elements = castDoc?.getElementsByTagName("HTML");
IEnumerator enumerator = elements?.GetEnumerator();
enumerator?.MoveNext();
var first = enumerator?.Current;
IHTMLElement2 castFirst = first as IHTMLElement2;
int? top = castFirst?.scrollTop;
return top;
}
I found kurt's answer almost worked but had to change the array reference as follows:
var document = (HTMLDocument)Browser.Document;
var scrollTop = (int)document.getElementsByTagName("HTML").item(0).ScrollTop;

Categories

Resources