arm
arm

Reputation: 451

Finding an element in Webdriver

I am trying to find test1 here but I cannot. Can someone tell me what am I doing wrong?

<button class="btn toggle-tab active" data-filter="test1">test1</button>
<button class="btn toggle-tab" data-filter="test2">test2</button>
<button class="btn toggle-tab" data-filter="test3">test3</button>
<button class="btn toggle-tab" data-filter="test4">test4</button

what I have tried is-

.//*[text()='test1']

.//title[text()='test1']

.//data-filter[text()="test1"]

Upvotes: 0

Views: 61

Answers (3)

Mayank Gupta
Mayank Gupta

Reputation: 171

Instead of using XPath to locate the element, try using CSS selectors. The CSS selector for test1 would be button[class$='active']. It is always better to use id, CSS selector or class name when locating elements.xpath is generally preferred in the worst case scenario, and fortunately, I have encountered just a few cases.

Upvotes: 0

Puneet Ghildiyal
Puneet Ghildiyal

Reputation: 21

Xpath to find linktext is - //htmltag[text()='linktext']

Here in your case, the xpath will be- //button[text()='test1']

Upvotes: 0

Bill Hileman
Bill Hileman

Reputation: 2838

"//button[contains(.,'test1')]"

There are other ways, but I prefer contains over equal (=) and in contains, I prefer "." over "text()"

You're effectively searching for the first tagname "button" which contains the text 'test1'

This question is more XPath-related, so you may want to include the xpath tag in your future posts that refer to them.

Upvotes: 1

Related Questions