I have used most of the element locator while Testing with selenium but very low frequently used 'TagName' locator. Please give and 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.
"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.
The Java Syntax for locating a web element using its Tag Name is written as: driver. findElement(By. tagName (<htmltagname>))
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.
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();
Thanks to the deprecation of By.tagName you should use By.css for @Shah 's answer....
String dropdown = driver.findElement(By.css("select")).getText();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With