Skip to main content

Behat Getting the tag name and tag attribute

HTML tags are like keywords, which defines that how web browser will format and display the content. HTML Tag Attributes are modifiers for the HTML tags and they provide additional information.

Consider below HTML:

<input class="search_query form-control ac_input" type="text" id="search_query_top" name="search_query" placeholder="Search" value="" autocomplete="off">

Here input is tag name and class, type, id, name, placeholder and autocomplete are its attribute.

Similarly,

<a class="login" href="/index.php?controller=my-account" rel="nofollow" title="Log in to your customer account">Sign in</a>

Here a is tag name and class, href, rel, title are its attribute.

Get Tag Name

You can get the tag name of the element using getTagName() method. 

Code:

$tag1 = $page->find('css','#search_query_top');
$tag2 = $page->find('css','.login');

echo "Tag 1 name is:" . $tag1->getTagName();
echo "\nTag 2 name is:" . $tag2->getTagName();

Output

Tag 1 name is:input
Tag 2 name is:a

Get Tag Attribute

We use getAttribute() method to get attribute of the tag.

Code:

$tag1 = $page->find('css','#search_query_top');
$tag2 = $page->find('css','.login');

echo "\nTag 1 class is:" . $tag1->getAttribute('class');
echo "\nTag 2 class is:" . $tag2->getAttribute('class');

echo "\nTag 1 placeholder is:" . $tag1->getAttribute('placeholder');
echo "\nTag 2 href is:" . $tag2->getAttribute('href');

output:

Tag 1 class is:search_query form-control ac_input
Tag 2 class is:login
Tag 1 placeholder is:Search
Tag 2 href is: /index.php?controller=my-account

Tags
Submitted by tgoswami on July 14, 2021

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.