Finding elements in Web Page - Part I
Finding elements using the id, name, class name, link text, partial link text and tag name.
i. Finding elements using the ID attribute:
To implement we will follow this url: https://testpages.herokuapp.com/styled/find-by-playground-test.html After loading this URL in your browser right click on the selected text and then click on Inspect as shown in the following screenshot:
The structure of the element will be showing as highlighted. In our case the id for the selected text is "p1". In Java the syntax for selecting the "p1" id will be following:
driver.findElement(By.id(<element ID>))
ii. Find elements using the name attribute
Right click on the selected text and click Inspect then we can find the name attribute for the selected text as shown in following screenshot:
Here the name attribute to located the selected element is "pName2". In Java the syntax for selecting the "pName2" id will be following:
driver.findElement(By.name(<element name>))
iii. Find elements using the class attribute
In our example web page, 26 elements with the same class name and that is "normal". So here we can not specify any one of the element only using className attribute. When we try to locate using className then by default first element will be picked.
But in the following element, we can find item with className as there is only one element with the className:
The syntax for finding element using className in Java will be as follows:
driver.findElement(By.className(<element name>))
iv. Find elements using the Link text:
Navigate to https://testpages.herokuapp.com/styled/index.html. Then find the link text of the element as shown in the screenshot below:
The syntax for finding element using linkText in Java will be as follows:
driver.findElement(By.linkText(<link text>))
v. Find elements using the Partial Link Text:
We can also find the element of previous element in (iv)using partial link text rather than using full link text.
The syntax for finding element using linkText in Java will be as follows:
driver.findElement(By.partialLinkText(<partial link text>))
vi. Find elements using the tag name:
Navigate to https://testpages.herokuapp.com/styled/tag/table.html. Then view the no of <tr> tag by inspecting table as shown in the following screenshot:
The syntax for finding element using linkText in Java will be as follows:
driver.findElement(By.tagName(<HTML tag name>))
Source code of all the ways of finding elements discussed above:
package findingElements; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class FindingElementsById { protected WebDriver driver; @Before public void setUp(){ System.setProperty("webdriver.chrome.driver", "./src/test/resources/chromedriver_version_87.exe"); driver = new ChromeDriver(); //Launch Chrome driver.manage().window().maximize(); //Maximize the browser window } @Test public void findElementsUsingId() throws InterruptedException{ driver.get("https://testpages.herokuapp.com/styled/find-by-playground-test.html"); System.out.println("Inside the findElementsUsingId function"); Thread.sleep(10); WebElement element = driver.findElement(By.id("p1")); String elementText = element.getText(); System.out.println("Locating using ID, Element text is: " + elementText); assertEquals("This is a paragraph text", elementText); } @Test public void findElementsUsingName() throws InterruptedException{ driver.get("https://testpages.herokuapp.com/styled/find-by-playground-test.html"); System.out.println("Inside the findElementsUsingName function"); Thread.sleep(10); WebElement element = driver.findElement(By.name("pName2")); String elementText = element.getText(); System.out.println("Locating using name, Element text is: " + elementText); assertEquals("This is b paragraph text", elementText); } @Test public void findElementsUsingClassName() throws InterruptedException{ driver.get("https://testpages.herokuapp.com/styled/find-by-playground-test.html"); System.out.println("Inside the findElementsUsingClassName function"); Thread.sleep(10); WebElement element = driver.findElement(By.className("page-navigation")); String elementText = element.getText(); System.out.println("Locating using class, Element text is: \n" + elementText); assertEquals("Index", elementText); } @Test public void findElementsUsingLinkText() throws InterruptedException{ driver.get("https://testpages.herokuapp.com/styled/index.html"); System.out.println("Inside the findElementsUsingLinkText function"); Thread.sleep(10); WebElement element = driver.findElement(By.linkText("Basic Web Page Example")); String elementText = element.getText(); System.out.println("Locating using linkText, Element text is: \n" + elementText); assertEquals("Basic Web Page Example", elementText); } @Test public void findElementsUsingPartialLinkText() throws InterruptedException{ driver.get("https://testpages.herokuapp.com/styled/index.html"); System.out.println("Inside the findElementsUsingPartialLinkText function"); Thread.sleep(10); WebElement element = driver.findElement(By.partialLinkText("Basic Web")); String elementText = element.getText(); System.out.println("Locating using Partial linkText, Element text is: \n" + elementText); assertEquals("Basic Web Page Example", elementText); } @Test public void findElementsUsingTagName() throws InterruptedException{ driver.get("https://testpages.herokuapp.com/styled/tag/table.html"); System.out.println("Inside the findElementsUsingTagName function"); Thread.sleep(10); WebElement table = driver.findElement(By.id("mytable")); List<WebElement> rows = table.findElements(By.tagName("tr")); System.out.println("No of rows = "+rows.size()); //String elementText = element.getText(); //System.out.println("Locating using Partial linkText, Element text is: \n" + elementText); assertEquals(5, rows.size()); } @After public void tearDown() throws Exception{ System.out.println("Closing the driver"); driver.quit(); } }
Create a class in eclipse and paste the code from above and run as Unit Test. You can check my previous post for detail set up process in Eclipse from here Getting Started with Selenium: Set up Selenium-Maven-Java Project in Eclipse
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন