When and how I can locate element by Tagname using selenium webdriver? Please explain with an example

Santosh  picture Santosh · Dec 6, 2015 · Viewed 60.8k times · Source

I have used most of the element locator while Testing with selenium but very low frequently used 'TagName' locator. Please give and example.

Answer

Shah picture Shah · Dec 6, 2015

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.

enter image description here

Here you can select the tagname as a locator like:

//Locating element by tagName and store its text in variable dropdown.
 String dropdown = driver.findElement(By.tagName("select")).getText();