How to Inspect the web elements of a web page?
- Every web page has different web elements.
- To identify the web element, use the developer tool to inspect the element.
- For that, Right-Click on the Web Page ➡ Select Inspect ➡ Select the Element tab. (or Shortcut Key F12)
- Every web element contains tags.
- Those tags contain attributes.
- Attributes contain values.
- Example: <span class="DPvwYc GHpiyd" aria-hidden="true">Hello</span>
Note: Web elements are common for all browsers.
What is findElement?
It is the method used to locate elements on
the webpage.
What is By?
By is the mechanism used to locate elements
within documents with the help of locator value.
What is a Locator?
It is an address that identifies a web
element uniquely within the web page.
Locators:
There are eight types of locators.
- id
- name
- xpath
- cssSelector
- className
- tagName
- linkText
- partialLinkText
- We can identify elements by using the ‘id’ attribute.
- Syntax: <object>.findElement(By.id("id_value"));
- Example: driver.findElement(By.id("dollar"));
- Example Program:
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class LocatorID {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- driver.findElement(By.id("dollar")).click();
- }
- }
name Locator:
- Name attribute can also identify an element.
- Example: driver.findElement(By.name(“value”));
xpath:
Xpath is an XML path used for navigation through the HTML structure of
the page.
Right-click on the tag of the particular field (on - DOM) ➡Select copy ➡ Select copy x path |
There are two types of XPath
1.
Absolute path:
a.
Full path from top to bottom /end.
b.
It starts with a single slash (/).
c.
Search from the root node
Example: /html/body/div/input/div[2]/ul/li[2]/……..
2.
Relative XPath:
a.
Middle of the path.
b.
It starts with a double slash (//).
c.
Search starts from the middle of the DOM
Example: //*[@id=’value’]
Example:
driver.findElement(By.xpath(“//input[@id=’value’]”))
- Example program:
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class LocatorXpath {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- WebElement elm = driver.findElement(By.xpath("//*[@id=\'dollar\']/a/span/b/span"));
- elm.click();
- }
- }
cssSelector:
CSS is used to create style rules for web pages and can be used to
identify any web element.
Right-Click on a particular element tag à
Select
Copy à
Select
Copy Selector |
- It starts with #.
- Xpath does not work in the Internet Explorer browser in that scenario they will use cssSelector.
- XPath and cssSelectors are both the same.
- Example: driver.findElement(By.cssSelector(“#input[@attribute]”))
className:
A ClassName operator uses a class
attribute to identify an object.
Example: driver.findElement(By.className(‘value’))
tagName:
In real-time we rarely use it. Here
we will assign the values based on the main tag, not an
attribute.
- Example: driver.findElement(By.tagName(“INPUT”));
Note: The tag name can be declared using either capital or lowercase letters.
- Example Program: Get the number of links on a web page using Selenium with Java?
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class Links {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://www.google.com");
- List<WebElement> l = driver.findElements(By.tagName("a"));
- System.out.println(l.size());
- driver.quit();
- }
- }
- //Output: 25
linkText:
Text (visible text) used in hyperlinks
can also locate elements.
Example: driver.findElement(By.linkText(“Gmail”));
- Example program:
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class LocatoeLinkText {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- WebElement elm = driver.findElement(By.linkText("C# vs Java"));
- elm.click();
- }
- }
partialLinkText:
Part of the text in the link can also
identify an element.
Example: link text:- click here to download this song
driver.findElement(By.partialLinkText(“click here”));
No comments:
Post a Comment