How to handle DOM in Selenium Webdriver using java.


Document Object Model(DOM): The Document Object Model (DOM) is an application programming interface (API). It defines the logical structure of documents and the way Javascript sees its containing pages' data. It is an object that includes how the HTML/XHTML/XML is formatted, as well as the browser state.
A DOM element is something like a DIV, HTML, BODY element on a page. You can add classes to all of these using CSS, or interact with them using JS.The DOM strategy works by locating elements that matches the JavaScript expression referring to an element in the DOM of the page.



HTML CODE

There are Three basic ways to locate an element through DOM
getElementById
The value of the ID attribute of the element to be accessed. This value should always be enclosed in a pair of parentheses ('').The most efficient way and preferred way to locate an element on a web page is By ID. ID will be the unique on web page which can be easily identified
Syntax: document.getElementById('id')
id=id of the element
Example:
WebElement email = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.getElementById('email');");
email.sendKeys("abcdef@gmail.com");

getElementsByName
Locates an element using the Class attribute.It collects an array of elements that have the name that you specified. You access the individual elements using an index which starts at 0.
Syntax: document.getElementsByName("name")[index]
name = name of the element as defined by its 'name' attribute
index = an integer that indicates which element within getElementsByName's array will be used.
Example:
WebElement last = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.getElementsByName('name1')[0];");
last.sendKeys("aaaaaa");

Forms
There are four way locate input form. The combination of form locators is given below.
Syntax 1: document.forms[index of the form].elements[index of the element]
Syntax 2: document.forms[index of the form].elements['name of the element']
Syntax 3: document.forms['name of the form'].elements[index of the element]
Syntax 4: document.forms['name of the form'].elements['name of the element']
index of the form = the index number (starting at 0) of the form with respect to the whole page.
index of the element = the index number (starting at 0) of the element with respect to the whole form that contains it.
name of the form =The name of the form as defined by its 'name' attribute and it applies only to elements within a named form.
name of the element: The name of element and defined by 'name' attribute.
Example:
WebElement pass = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.forms['domf2'].elements['password'];");
pass.sendKeys("aaaaaa");

WebElement btn = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.forms[1].elements['butt'];");
btn.click();

Working Demo:-
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import org.openqa.selenium.JavascriptExecutor;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.NoSuchElementException;  
 public class DOMhandlerProgram {  
 public static void main(String[] args) throws InterruptedException {  
     try {  
 WebDriver driver = new FirefoxDriver();  
 //Maximize window  
 driver.manage().window().maximize();  
 //Launching the browser page  
 driver.get("file:///C:/Users/Hiro%20Mia/Desktop/Blog%20content/DOM%20locator.html");  
 //Adding wait  
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
 
//Creating the Javascriptexecutor interface object by Type casting  
 JavascriptExecutor js = (JavascriptExecutor) driver;  
 //Send email address   
 WebElement email = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.getElementById('email');");  
 email.sendKeys("abcdef@gmail.com");  
 Thread.sleep(3000);  

 //send pass  
 WebElement pass = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.forms['domf2'].elements['password'];");  
 pass.sendKeys("aaaaaa");  
 Thread.sleep(3000);  

 //Click on button  
 WebElement btn = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.forms[1].elements['butt'];");  
 btn.click();  
 WebElement last = (WebElement) ((JavascriptExecutor) driver).executeScript("return document.getElementsByName('name1')[0];");  
 last.sendKeys("aaaaaa");  
 Thread.sleep(3000);  

 //close firefox browser   
 driver.quit();  
     } catch (NoSuchElementException e) {  
     }  
   }  
 }  

12 comments:


  1. Thank you for the info. It sounds pretty user friendly. I guess I’ll pick one up for fun. thank u.

    Selenium Training in Chennai

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hey great tool...and nice explanation...Very useful anu quick understand

    Best Selenium Training in Chennai

    ReplyDelete
  4. Learn Selenium from our Experts in IT industry. We are the best providers of
    Selenium Training in Chennai with excellent syllabus. By placement, course syllabus and practicals we are the
    BEST Selenium Training in Chennai.
    Contact Us:
    Selenium training institute in Chennai

    ReplyDelete
  5. Excellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative.We are also providing the best services click on below links to visit our website..I have one more information related with roblox..

    ReplyDelete
  6. Your provided blog explains the Document Object Model (DOM) and demonstrates how to use different DOM locators to interact with web elements using JavaScriptExecutor in Selenium WebDriver. Overall, the blog provides a useful introduction to DOM and its practical application in automating web testing scenarios.
    Software Testing Trends To Look Out For In 2023

    ReplyDelete