0% found this document useful (0 votes)
7 views

Week 5 Practicing DOM and Element Interaction

Uploaded by

Haris Ramay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Week 5 Practicing DOM and Element Interaction

Uploaded by

Haris Ramay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PRACTICING DOM AND

ELEMENT INTERACTION
WORKING WITH ELEMENT
METHODS

1. Use the findElement() method to locate a single web element by ID.


2. Use the findElements() method to locate multiple web elements by
ID.
3. Use the wait() method to ensure the web element is present and
visible before interacting with it.
4. Use the getText() method to get the text value of a web element.
5. Use the sendKeys() method to enter text into an input field.
6. Use the click() method to click on a web element.
7. Use the isSelected() method to check if a checkbox or radio button is
selected.
8. Use the isEnabled() method to check if a web element is enabled.
WORKING WITH ELEMENT METHODS –
EXAMPLES
URL: HTTPS://WWW.DARAZ.PK/#
WORKING WITH ELEMENT METHODS –
EXAMPLES
URL: HTTPS://WWW.DARAZ.PK/#
WORKING WITH ELEMENT METHODS –
EXAMPLES
URL: HTTPS://WWW.DARAZ.PK/#
XPATH AXES

• XPath axes are used to navigate through the XML


document's structure, allowing you to select elements in
relation to a given node.
• XPath Axes are used for finding dynamic elements when
normal XPath element search methods like name, ID,
class name, etc., aren't possible.
• They allow you to locate elements based on their
relationship with other elements, like parent, sibling,
child, ancestor, or descendant.
• Here are some of the commonly used XPath Axes:
COMMONLY USED XPATH AXES

• ancestor: Selects all ancestors (parent, grandparent, others.) of the


current node.
• child: This selects all children of the current node.
• descendant: Selects all descendants (children, grandchildren, others.) of
the current node.
• following: This selects everything in the document after the closing tag of
the current node.
• following-sibling: This selects all siblings after the current node.
• parent: This selects the parent of the current node.
• preceding: This selects all nodes that appear before the current node in
the document, excluding ancestors and attributes.
• preceding-sibling: This selects all siblings before the current node.
• self: This selects the current node.
• attribute: This selects the attributes of the current node.
C O M M O N LY U S E D X PAT H A X E S - E X A M P L E S
URL:
HT T P S : / / AU TO M AT I O N E X E R C I S E . C O M / C O N TAC T _ U S

• 1. Ancestor: Selects all ancestors (parent, grandparent, others) of the current


node.
// Example: Select all ancestor elements of a specific node
List<WebElement> ancestors =
driver.findElements(By.xpath("//input[@name='name']/ancestor::*"));
System.out.println("Ancestor elements count: " + ancestors.size());

• 2. Child: Selects all children of the current node.


// Example: Select all child elements of a specific node
List<WebElement> children =
driver.findElements(By.xpath("//form[@id='contact-us-
form']/child::*"));
System.out.println("Child elements count: " + children.size());
C O M M O N LY U S E D X PAT H A X E S - E X A M P L E S
URL:
HT T P S : / / AU TO M AT I O N E X E R C I S E . C O M / C O N TAC T _ U S

• 3. Descendant: Selects all descendants (children, grandchildren, others) of the


current node..
// Example: Select all descendant elements of a specific node
List<WebElement> descendants =
driver.findElements(By.xpath("//div[@id='form-
section']/descendant::*"));
System.out.println("Descendant elements count: " +
descendants.size());

• 4. Following: Selects everything in the document after the closing tag of the
current node.

// Example: Select all following elements after a specific node


List<WebElement> followingElements =
driver.findElements(By.xpath("//input[@name='name']/following::*"));
System.out.println("Following elements count: " +
followingElements.size());
C O M M O N LY U S E D X PAT H A X E S - E X A M P L E S
URL:
HT T P S : / / AU TO M AT I O N E X E R C I S E . C O M / C O N TAC T _ U S

• 5. Following-Sibling: Selects all siblings after the current node.


// Example: Select all following sibling elements of a specific node
List<WebElement> followingSiblings =
driver.findElements(By.xpath("//input[@name='name']/following-
sibling::*"));
System.out.println("Following sibling elements count: " +
followingSiblings.size());

• 6. Parent: Selects the parent of the current node. (Use Highlight Method from Week 4
for this)

// Example: Select the parent element of a specific node


WebElement parent =
driver.findElement(By.xpath("//input[@name='name']/parent::*"));
System.out.println("Parent tag: " + parent.getTagName());
C O M M O N LY U S E D X PAT H A X E S - E X A M P L E S
URL:
HT T P S : / / AU TO M AT I O N E X E R C I S E . C O M / C O N TAC T _ U S

• 7. Preceding: Selects all nodes that appear before the current node in the
document, excluding ancestors and attributes.

// Example: Select all preceding elements before a specific node


List<WebElement> precedingElements =
driver.findElements(By.xpath("//input[@name='name']/preceding::*"));
System.out.println("Preceding elements count: " +
precedingElements.size());

• 8. Preceding-Sibling: Selects all siblings before the current node.

// Example: Select all preceding sibling elements of a specific node


List<WebElement> precedingSiblings =
driver.findElements(By.xpath("//input[@name='name']/preceding-
sibling::*"));
System.out.println("Preceding sibling elements count: " +
precedingSiblings.size());
C O M M O N LY U S E D X PAT H A X E S - E X A M P L E S
URL:
HT T P S : / / AU TO M AT I O N E X E R C I S E . C O M / C O N TAC T _ U S

• 9. Self: Selects the current node itself. (Use Highlight Method from Week 4 for this)

// Example: Select the current node itself


WebElement self =
driver.findElement(By.xpath("//input[@name='name']/self::*"));
System.out.println("Self element tag: " + self.getTagName());

• 10. Attribute: Selects the attributes of the current node. (Use Highlight Method from
Week 4 for this)

// Example: Get the value of a specific attribute of the node


String attributeValue =
driver.findElement(By.xpath("//input[@name='name']")).getAttribute("pl
aceholder");
System.out.println("Attribute value (placeholder): " +
attributeValue);
INTELLIJ PROJECT HELP

• An intelliJ project named “W5 Xpath Axes.zip” has been uploaded under the
course material section.
• Download, unzip, and then open it in the IntelliJ for practice as shown and
done during the class.
INTERACTING WITH WEB
ELEMENTS

• Objective: Learn how to interact with various basic web elements using
Selenium with Java.
• Key Web Elements:
• Dropdowns: Selecting options from a dropdown menu.
• Checkboxes: Interacting with checkboxes and verifying selection.
• Radio Buttons: Selecting radio buttons and checking their status.
• Tables: Extracting data from tables.
• Images: Checking image display and attributes.
• Capturing Screenshots: Taking screenshots of elements or pages.
• Date Pickers: Selecting dates from date pickers.
• File Upload/Download: Interacting with file upload and download elements.
INTELLIJ PROJECT HELP

• An intelliJ project named “W5 Web Elements Practice.zip” has been uploaded
under the course material section.
• Download, unzip, and then open it in the IntelliJ for practice as shown and
done during the class.
• Notes:
• Practice: Uncomment different sections of the code to practice handling each type
of element.
• Explore: Use the demo websites provided in the code examples to explore more
web elements and their interactions.

You might also like