Anuwa
Anuwa

Reputation: 9

Identify objects in selenium

In my application, it has several tabs, say 'AAA','ABC','ADF'; I need to automate the click on tab 'ABC'.

Those tabs have ids and they are 'tab1','tab2','tab3'. I can done this easily by using ids. but i don't want to use this because those tabs will change time to time. so I need to use the name in the tab, because it is unique.

Below is my tag:

<a id="tab2" class="current" onclick="expandcontent('sc2', this);" href="#"> ABC </a>

Upvotes: 1

Views: 1876

Answers (5)

Brian
Brian

Reputation: 5119

As Arran mentioned, you are likely best off searching for the tab names using XPath but, you can also use FindBy as well. An example of this would be:

@FindBy(css=<the CSS value for the tab>)     private WebElement pageTab2;
@FindBy(id="tab2")                           private WebElement pageTab2;

I can only agree with what Arran also mentioned about your developers using more relevant naming conventions as well. The easier they make your job, the more you can do to make their lives easier too.

Upvotes: 3

akhilesh gulati
akhilesh gulati

Reputation: 128

xpath=//a[contains(@class,"current") and contains(@text,"ABC")]

Upvotes: 0

Stewart Moon
Stewart Moon

Reputation: 279

The simplest approach would be to use X paths to find your tabs.

When you are starting out there are two helpful tools to help you find X paths. Download Mozilla Firefox and get these two add-ons:

  • Web Driver Element Locator-After downloading you can right click a web element and select X path.
  • Selenium IDE-This is a recorder and will give you the X paths or CSS of each web element you interact with.

I would recommend starting with these tools to help you out since you are just starting out.

As for your question: I would recommend using what @Arran said...

//a[text()='ABC']

'a'-will search the entire page for whatever text you have in single quotes ''

If you Right Click the element or Tab and select Inspect. You will then be looking through the back-end of the page for classes or div that help you identify each different element.

Upvotes: 0

Virendra Joshi
Virendra Joshi

Reputation: 479

Simplest answer will be Use..."link=ABC" or link=" ABC " (it there are leading and trailing spaces).

Upvotes: 1

Arran
Arran

Reputation: 25066

If this is the case, you will probably need to fall back to XPath and perform text-based searches, for instance:

//a[text()='ABC']

Though, I'd advise you work with your development team to have consistent ID's. Text based matching is fine, but when you start to use older browsers you'll notice it really really slows down the tests.

However, you can also use the .LinkText and .PartialLinkText selectors in your language API's - there should be an implementation of those selectors in each API (C#, Ruby, Python etc). The catch here is this will be for a (anchor) elements only. However, providing that is the only type of elements this needs to be done by, you can get away with using this instead of XPath.

Upvotes: 5

Related Questions