Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
The following sample code demonstrates how to access, list and modify attributes of the particular DOM element.
Note:
Element attributes cannot be used to modify element properties. The difference between attributes and properties is explained here:
http://stackoverflow.com/questions/6003819/properties-and-attributes-in-html
C#
using DotNetBrowser; using DotNetBrowser.DOM; using DotNetBrowser.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DOMGetAttributesSample { class Program { static void Main(string[] args) { Browser browser = BrowserFactory.Create(); browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e) { if (e.IsMainFrame) { //Get the document DOMDocument document = e.Browser.GetDocument(); //Get the element by its Id DOMElement link = document.GetElementById("link"); //Get the attributes of the received element Dictionary<String, String> attributes = link.Attributes; //Print attributes foreach (var attribute in attributes) { Console.WriteLine(attribute.Key + " = " + attribute.Value); } //Update the style attribute value link.SetAttribute("style", "background-color: blue;"); } }; browser.LoadHTML("<html><body><a href='#' id='link' title='Link title' style='background-color: red;'></a></body></html>"); } } }
VB.NET
Imports System.Collections.Generic Imports DotNetBrowser Imports DotNetBrowser.DOM Module Module1 <STAThread> Sub Main() Dim browser As Browser = BrowserFactory.Create() AddHandler browser.FinishLoadingFrameEvent, Sub(o, e) If e.IsMainFrame Then 'Get the document Dim document As DOMDocument = e.Browser.GetDocument() 'Get the element by its Id Dim link As DOMElement = document.GetElementById("link") 'Get the attributes of the received element Dim attributes As Dictionary(Of String, String) = link.Attributes 'Print attributes For Each attribute In attributes Console.WriteLine(attribute.Key + " = " + attribute.Value) Next 'Update the style attribute value link.SetAttribute("style", "background-color: blue;") End If End sub browser.LoadHTML( "<html><body><a href='#' id='link' title='Link title' style='background-color: red;'></a></body></html>") End Sub End Module