Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an element with Xpath with an attribute and w/o another attribute

This is my Xpath query:

driver.findElements(By.xpath("//*[@id=\"continue-button\" and not(contains(@class=\"disabled-button\"))]"));

I am having trouble locating the element with the id of continue-button and not the class of disabled-button. I am obviously writing the query wrong, but I am not sure of the issue. I am unfamiliar with the Xpath syntax to use in this situation.

like image 360
user2121620 Avatar asked Sep 19 '13 20:09

user2121620


People also ask

What is the meaning of '/' in XPath?

Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

What does * indicate in XPath?

The XPath default axis is child , so your predicate [*/android.widget.TextView[@androidXtext='Microwaves']] is equivalent to [child::*/child::android.widget.TextView[@androidXtext='Microwaves']] This predicate will select nodes with a android. widget. TextView grandchild and the specified attribute.


1 Answers

Your xpath:

//*[@id=\"continue-button\" and not(contains(@class=\"disabled-button\"))]

Should be:

//*[@id="continue-button"][not(contains(@class, "disabled-button"))]

I highly suggest if you use Firebug to install something like firepath to test out your xpath expressions before running them in tests.

Edit: For anybody who may see this in the future, tools like Firebug are no longer necessary. Newer versions of browsers now allow you to enter XPath and CSS expressions in their respective DOM-viewer. For example in Chrome, the hotkey F12 will open the debug tool, and the hotkey Ctrl+F while on the "Elements" tab will open a search bar which you may enter selector expressions into.

like image 97
NaviSaysListen Avatar answered Nov 08 '22 03:11

NaviSaysListen