LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I use TagName locator in Selenium automation scripts?

Can anyone please tell me how can I use TagName locator in Selenium automation scripts.

Up Vote Down Vote 0 Votes
Posted 4 months ago

Answers


The TagName locator locates an element in a web page by the tag name of the element, for example "input", "select", "button", ...

If there is no other way to locate the element unambiguously, you cal locate it by the tag name.

Suppose that on your web page you have only one input, on which you want to input some text. Then you can locate the element like this, and then type text in it::

var myinput = dialog.FindElement(By.TagName("input"));

myinput.send_keys('sometext')

If you have several inputs on the page, and you know you want to access the first, you can use findelements (plural), and then access the first element:

var myinputsarray = dialog.FindElements(By.TagName("input"));

myinputsarray[0].send_keys('sometext')

If you don’t know exactly which of the several inputs you want to access, you have to find another way to unambiguously define the element, perhaps by using XPath.

Up Vote Down Vote 0 Votes
Posted 2 months ago