Behat Getting the tag name and tag attribute

Profile picture for user devraj

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