Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath syntax for "and not contains"

Tags:

xpath

qtp

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.

like image 873
Matt Avatar asked Oct 29 '14 21:10

Matt


People also ask

Can we use and with Contains in XPath?

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.

What is the syntax of XPath?

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.

HOW include XPath with Contains in Selenium?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]


2 Answers

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')]
like image 177
Daniel Haley Avatar answered Oct 04 '22 20:10

Daniel Haley


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.

like image 35
Michael Kay Avatar answered Oct 04 '22 20:10

Michael Kay