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.
Related
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.
I have a Treeview created in virtual mode which have 4 levels of nodes and at the page load the root element and first level elements are loaded and rest will be loaded based on dynamically using treeFolderList_VirtualModeCreateChildren event.
This is what I have so far
protected void treeFolderList_VirtualModeCreateChildren(object source, TreeViewVirtualModeCreateChildrenEventArgs e)
{
List<TreeViewVirtualNode> children = new List<TreeViewVirtualNode>();
if (e.NodeName == null)
{
AppendChildNode(children, "root", "All Domains", false);
}
else
{
if (e.NodeName.Contains("root"))
{
PopulateChildNodes(false, children);
}
else
{
if (!(e.NodeName.StartsWith("u_")))
{
PopulateUserChildNodes(false, children, GetDomainBase(e.NodeName), e.NodeName);
}
else
{
TreeViewVirtualNode tvNode = (TreeViewVirtualNode)treeFolderList.Nodes.FindByName(e.NodeName);
TreeViewVirtualNode tvNodeParent = (TreeViewVirtualNode)tvNode.Parent;
string tvParentNodeName = tvNodeParent.Name;
PopulateUserChildNodes(true, children, GetDomainBase(tvParentNodeName), e.NodeName);
}
}
}
e.Children = children;
}
This work as expected and it creates the children elements when expanding nodes respectively. My problem is I have check boxes for each node and I need to be able to save the Treeview in a way when I reload/redirect to the page it would reflect the nodes I have selected.
Is there a way to achieve this?
There are several ways to try to achieve this:
Configure ASPxTreeList.SettingsCookies element (probably the easiest solution), in particular the StoreSelection attribute:
<dvx:ASPxTreeList ... >
...
<SettingsCookies Enabled="true" StoreSelection="true" />
...
</dvx:ASPxTreeList>
If SettingsCookies doesn't work try saving and restoring the TreeList layout manually using ASPxTreeList.ClientLayout event. Define the event handler first:
<dvx:ASPxTreeList OnClientLayout="dvxTreeList_ClientLayout"... >
...
</dvx:ASPxTreeList>
and follow the example in the doc to handle this event. This way ASPxTreeView as well as the ASPxGridView provide the node/column format data in the e.LayoutData string which can be saved to DB and then restored back.
Store the TreeView selected node keys in Session and restore them using callbacks:
Example: ASPxTreeList - How to store the selection between requests
I personally would not rely on cookies and would try methods #2 and #3 first. #2 has been working nicely for me with ASPxGridView and #3 we use in a complex ASPxTreeView setup which also tracks the selection of hidden nodes.
I hope the examples in the linked docs should be easy enough for you to copy and modify. If not, comment what is not working for you.
HTH
I need to click the button "Add" in the post new wordpress form, this button is to add tags to the post , the trouble is that button don't have the value and id propertie. Is just like that
the html for the button
input type="button" class="button tagadd" value="Add" tabindex="3"
my tries
webBrowser1.Document.GetElementById("button tagadd").InvokeMember("click");
webBrowser1.Document.GetElementById("Add").InvokeMember("click");
"GetElementById without id"
:-)
Unless you can change the markup for the button
What you need now is to traverse the entire DOM and look for a button in a known place. I'd suggest adding jquery if not already exist to be able for easier dom manipulation/search.
If you add jquery you could do something like $(".tagadd").click()
You could try doing
webBrowser1.document.getElementsByClassName("tagadd")
EDIT: Here is a script to create the getElementsByClassName function if it's not available http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
There is also this http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx but I've never used it.
Add an ID. Even if you're dynamically generating the buttons this should be trivial.
If you're using jQuery,
$('.tagadd')
will return a collection of everything with the tagadd class applied. You can further filter this by the other classes (button, etc)
Use this:
onload=function(){
if (document.getElementsByClassName == undefined) {
document.getElementsByClassName = function(className)
{
var hasClassName = new RegExp("(?:^|\s)" + className + "(?:$|\s)");
var allElements = document.getElementsByTagName("*");
var results = [];
var element;
for (var i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass
&& elementClass.indexOf(className) != -1
&& hasClassName.test(elementClass))
results.push(element);
}
return results;
}
}
}
and another
Some browsers provide the method getElementsByClassName() which lets you select by class without using jQuery (which is a bit heavy if this is all you need). I haven't tested this so I'm not sure how widely it's supported.
Did I mention that you should give everything an ID?
use TagName isteed for example
var elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("class") == "button tagadd")
{
elem.InvokeMember("click");
}
}
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
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