Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 743
Santosh Avatar asked Dec 06 '15 09:12

Santosh


People also ask

What is tagName in Selenium example?

A tag name is a part of a DOM structure where every element on a page is been defined via tag like input tag, button tag or anchor tag etc. Each tag has multiple attributes like ID, name, value class etc. As far as other locators in Selenium are concerned, we used these attributes values of the tag to locate elements.

What is tagName locator in Selenium?

"tagName" is one of the 8 locators supported by selenium. For instance, display all the anchors "a" or "images" alternative texts on amazaon india page @ https://www.amazon.in/ selenium identifies the "a" and "image" tags with the following java statements. List<WebElement> links = driver.

How do I search for an element in tagName?

The Java Syntax for locating a web element using its Tag Name is written as: driver. findElement(By. tagName (<htmltagname>))


2 Answers

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();
like image 35
Shah Avatar answered Oct 28 '22 09:10

Shah


Thanks to the deprecation of By.tagName you should use By.css for @Shah 's answer....

String dropdown = driver.findElement(By.css("select")).getText();
like image 185
JGleason Avatar answered Oct 28 '22 09:10

JGleason