Here is the XPath I'm trying to use:
//div[contains(@class='xyz ng-binding ng-scope') and not(contains(@class = 'ng-hide'))]
I'm not sure what the correct syntax for this is. Basically the HTML looks like like:
class="xyz ng-binding ng-scope typeA ng-hide"
class="xyz ng-binding ng-scope typeB ng-hide"
I want to select the case where the HTML is either typeA
or typeB
but does not have ng-hide
.
Using XPath- contains() method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath(“//*[contains(@id,'lst-ib')]”)); Using the 'contains' function, we can extract a list of web elements containing the matching text throughout the web page.
Syntax of XPathtagname: Name of the tag of a particular node. @: Used to select the select attribute. Attribute: Name of the attribute of the node. Value: Value of the attribute.
The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]
You could do something like this:
//div[(contains(@class,'typeA') or contains(@class,'typeB')) and not(contains(@class,'ng-hide'))]
You should also take a look at How can I match on an attribute that contains a certain string? to see why the contains()
above might not match exactly what you're intending.
For example, to truly match what you are intending, you could use:
//div[(contains(concat(' ',@class,' '),' typeA ') or contains(concat(' ',@class,' '),' typeB ')) and not(contains(concat(' ',@class,' '),' ng-hide '))]
It's much easier in XPath 2.0, but I'm not sure if qtp supports it. If you could make concat(' ',@class,' ')
a variable, you could clean up the XPath too.
Here's a 2.0 example just in case:
//div[tokenize(@class,'\s')=('typeA','typeB') and not(tokenize(@class,'\s')='ng-hide')]
contains()
is a function that takes two arguments: contains(A, B) returns true if B is a substring of A.
So your syntax would become valid if you replaced your "=" with ",": contains(@class, 'X')
in place of contains(@class = 'X')
. But I don't know whether it would then do what you want against all possible input data - that's a different question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With