How to get any attribute if no default method is defined for the same?

While analyzing selenium, I got a problem of retrieving a particular property as no default method was defined for retrieving the same in the Selenium API. The solution for the same was a little tricky. Let me explain the same with an expample.

Example: Suppose in a page there is a link called “your link”. When hover your mouse over that particular link, a title tag is displayed saying “This link opens in new window”. Now the problem is you will have to test is the text of the title tag is correct or not. To do the same, we need to retrive the title tag of the link using locator for the link, let’s say we’ll take the xPath of the link. But there is no default method to retrieve the title tag in Selenium API. To get this title tag now, we’ll have to use the getAttribute() method. The method to get it is as below.

Let’s say the xPath of the link is: link=your link
To get the title tag the xPath needs to be appended with the attribute with ‘@’ at the end.
So, the new xPath becomes: link=your link@title
Let’s say, a local variable xpath has this xPath value. So, when we call selenium.getAttribute(xpath) now, it will return the text which the title attribute of link contains, in this case “This link opens in new window”.

Similarly another such example would be to get the alt property of an image in a page.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s