Element identification methods are common in all languages like Java, C#.Net etc with some syntactical differences.
We can identify the web elements in selenium web driver using below methods.
driver.findElement(By.tagName("TD"));
Above method will return the first element with given tag - TD.
Example -
WebElement e = driver.findElement(By.tagName("div"));
Above line will get the first element whose tag is div.
To get all div elements, you can use below code
List <WebElement> we_collection = driver.findElements(By.tagName("div"));
Please note that to get the single element we use findElement method and
to get the collection of elements we use findElements method
You can also access the element if you know its class name by using below code
driver.findElement(By.className("box"));
You can access the element if you know its id by using below code
driver.findElement(By.id("selenium"));
You can also access the element if you know the value of name attribute by using below code
driver.findElement(By.name("sagar"));
To find the links in web page, there are 2 ways as mentioned below.
driver.findElement(By.partialLinkText("news"));
driver.findElement(By.linkText("hello"));
If you know the part of the name of link, you can use By.partialLinkText
If you know the whole link name, you can use By.linkText
You can also access the element if you know its xpath expression by using below code
driver.findElement(By.xpath("//table"));
You can also access the element if you know its css expression by using below code
driver.findElement(By.cssSelector("#kid"));
To find all elements you can use below syntax.
List<WebElement> cells = tr.get(1).findElements(By.tagName("td"));
We can identify the web elements in selenium web driver using below methods.
- By Xpath
- By Css Selectors
- By Id
- By Name
- By Tag Name
- By Class Name
- By Linktext
- By Partial Link text
Out of these methods, xpath and css selector methods are very popular and useful. We can use xpath and css selectors to identify all kinds of elements in the webpage.
We can also identify the elements using Id, Name, Class Name provided that given element is assigned some id, name or class name.
We can also identify the elements using the html tags like table, div, form etc.
For identifying the links, selenium web driver provides 2 additional methods like link text and Partial link text.
Web Element Identification Examples in Java -
Above method will return the first element with given tag - TD.
Example -
WebElement e = driver.findElement(By.tagName("div"));
Above line will get the first element whose tag is div.
To get all div elements, you can use below code
List <WebElement> we_collection = driver.findElements(By.tagName("div"));
Please note that to get the single element we use findElement method and
to get the collection of elements we use findElements method
You can also access the element if you know its class name by using below code
driver.findElement(By.className("box"));
You can access the element if you know its id by using below code
driver.findElement(By.id("selenium"));
You can also access the element if you know the value of name attribute by using below code
driver.findElement(By.name("sagar"));
To find the links in web page, there are 2 ways as mentioned below.
driver.findElement(By.partialLinkText("news"));
driver.findElement(By.linkText("hello"));
If you know the part of the name of link, you can use By.partialLinkText
If you know the whole link name, you can use By.linkText
You can also access the element if you know its xpath expression by using below code
driver.findElement(By.xpath("//table"));
You can also access the element if you know its css expression by using below code
driver.findElement(By.cssSelector("#kid"));
To find all elements you can use below syntax.
List<WebElement> cells = tr.get(1).findElements(By.tagName("td"));
Above code will select all TD elements from the webpage.
Please note that some automation engineers use different terminologies for element identification like object identification methods or Selenium locators etc. In UFT, we use object repository to store the objects. In Selenium, we do not have separate file to store the objects or elements. But we can create our custom file which may store information about objects and their locators.
You may also like to read below topics.
What do you think on above selenium topic. Please provide your inputs and comments. Thanks
Please note that some automation engineers use different terminologies for element identification like object identification methods or Selenium locators etc. In UFT, we use object repository to store the objects. In Selenium, we do not have separate file to store the objects or elements. But we can create our custom file which may store information about objects and their locators.
You may also like to read below topics.
- Press key in Selenium Webdriver in Java
- Read Table Data in Selenium Webdriver in Java
- Take the screenshot in Selenium Webdriver in Java
- Execute JavaScript in Selenium Webdriver in Java
- Handle multiple browser windows in Selenium Webdriver in Java
- Double-Click on the Element in Selenium Webdriver in Java
- Exceptions in Selenium Webdriver in Java
- Synchronization in Selenium Webdriver in Java
No comments:
Post a Comment