SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Element Locators In Selenium 2 or WebDriver - Locating Element By Tag Name

We have learn about Locating element by ID and Locating element by Class Name for application software web page with examples in my previous posts. Let me repeat once more that if software web page's element contains ID then we can locate element by its ID and if software web page's element do not have ID and contains Class Name then we can locate an element by Class Name.

Now supposing, software web element do not have any ID or Class Name then how to locate that element in selenium WebDriver ? Answer is there are many alternatives of selenium WebDriver element locators and one of them is Locating Element By Tag Name.

Locating Element By Tag Name is not too much popular because in most of cases, we will have other alternatives of element locators. But yes if there is not any alternative then you can use element's DOM Tag Name to locate that element in webdriver.


Look in to above image. That select box drop down do not have any ID or Class Name. Now if we wants to locate that element then we can use it's DOM tag name 'select' to locate that element. Element locator sysntex will be as bellow.

driver.findElement(By.tagName("select"));

Bellow given example will locate dropdown element by it's tagName to store its values in variable. Copy bellow given @Test method part and replace it with the @Test method part of example given on this page.(Note : @Test method is marked with pink color in that example).

 @Test
 public void test() 
  {
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  //Locating element by tagName and store its text in to variable dropdown.
   String dropdown = driver.findElement(By.tagName("select")).getText();
   System.out.print("Drop down list values are as bellow :\n"+dropdown);
  }

No comments:

Post a Comment