Using "not" in selenium xpath [duplicate] - selenium

I want to write something of the sort:
//a[not contains(#id, 'xx')]
(meaning all the links that there 'id' attribute doesn't contain the string 'xx')
I can't find the right syntax.

not() is a function in XPath (as opposed to an operator), so
//a[not(contains(#id, 'xx'))]

you can use not(expression) function
or
expression != true()

None of these answers worked for me for python. I solved by this
a[not(#id='XX')]
Also you can use or condition in your xpath by | operator. Such as
a[not(#id='XX')]|a[not(#class='YY')]
Sometimes we want element which has no class. So you can do like
a[not(#class)]

Use boolean function like below:
//a[(contains(#id, 'xx'))=false]

Related

Qlikview -- Using If with a variable in expression

I am trying to use an if statement with a variable in my expression and I get no results. The variable works when I use the variable on it's own but when used with the if I get no results.
I have tried:
if(OrderQtr='Apr-Jun 2018',$(vAvgOrderCost),0)
if(OrderQtr='Apr-Jun 2018',sum($(vAvgOrderCost)),0)
sum($(vAvgOrderCost)if(OrderQtr='Apr-Jun 2018',0))
Nothing seems to work. Thanks
Variables in QlikView are used as a text replace feature, so be carefull. If your variable hold a value like 1,345 an expression like "if(OrderQtr='Apr-Jun 2018',$(vAvgOrderCost),0)" will be translated into "if(OrderQtr='Apr-Jun 2018',1,345,0)" which by itself will be a syntax error.
Something like :
Num(if(OrderQtr='Apr-Jun 2018','$(vAvgOrderCost)','0'))
would be a safe way to go.
the if() syntax should work like this if(test,true,false)
So looking at your examples I suspect this is what you are trying to do
sum(if(OrderQtr='Apr-Jun 2018',$(vAvgOrderCost),0))

LIKE operator in XQL

I am doing some XML parsing in webMethods which only supports XQL, not XPath/XQuery.
I'm trying to find the LIKE operator to do a wildcard search on an attribute value
/MAP[#MODE='INPUT']/MAPSET[#FIELD LIKE '/documentTypeName*']/DATA/Values/value
In XQuery, I was using the matches() function. Have been surprised at my lack of finding an answer through searching. My best bet was http://www.ibiblio.org/xql/xql-proposal.html which says it should be keyword contains but this doesn't seem correct.
Pretty much a necro, but i believe you are looking for 'icontains'.
http://www.ibiblio.org/xql/xql-proposal.html#Comparisons
Section 1.4.3 Comparisons. This should be a case insensitive version of 'contains'
(field icontains "/documentTypeName*")

how to locate dynamic element using xpath(ends-with function not working)

I have used ends-with function as
(By.xpath("//input[ends-with(#id,'_PHONE$']"));
But it didnot work
The ends-with function is part of XPath 2.0 but browsers generally only support 1.0.
So, if you strictly want to fetch all elements that ends with a specific pattern, you can either fetch all elements that contain that pattern (using the contains() function) and then strictly check the suffix of the id (fetch the attribute value by .getAttribute("id")) using Java's .endsWith() method.
Or
You can use string-length function, substring function and equals to get this XPath:
"//input[substring(#id, string-length(#id) - string-length('_PHONE$1') +1) = '_PHONE$1']"
driver.findElement(By.xpath("//id[contains(text(),'PHONE$')]"));
You can use below XPath as well:-
//input[contains(#id,'PHONE$1')]
Hope it will help you :)
Your id is ending with _PHONE$1 and not _PHONE$. Notice that there is a 1 at the end.
(By.xpath("//input[ends-with(#id,'_PHONE$1']"));
If you still don't want to match that 1 use contains.
By.xpath("//input[contains(#id,'_PHONE$1']"));
Use contains Method
By.xpath("//input[contains(text(), '_PHONE$']");
Or use wait explicit method

Using XPath wildcards in attributes in Selenium WebDriver

I want to use Wildcards in my attributes. For example, this is my regular XPath:
//input[#id='activation:j_idt84:voId:1']`
I want to replace the j_idt number with a wildcard because the number is dynamic. I'm looking for something like this:
//input[#id='activation:*:voId:1']
I don't know how to solve that issue. Is my idea even possible?
Unfortunately there's no string wildcards in XPath. However you can use multiple contains() and starts-with() to filter things like this.
//input[starts-with(#id, 'activation:') and contains(#id, ':voId:1')]
Also, this answer could be useful too: selenium: Is it possible to use the regexp in selenium locators
You can use string wildcards using the matches function which is available in XPath 2.0:
//input[matches(#id, 'activation:.*:voId:1')]

xPath last select element

Can someone help me to bring this code working? I have several select fields and I only want the last one in my variable.
variable = browser.elements_by_xpath('//div[#class="nested-field"]//select[last()]
Thanks!
This is a FAQ: The [] operator in XPath has higher precedence (priority) than the // pseudo-operator. This is why brackets must be used to change the default operator priorities. There are at least several similar questions with good explanations -- search for them and read and understand.
Instead of:
//div[#class="nested-field"]//select[last()]
Use:
(//div[#class="nested-field"]//select)[last()]
is the class attribute an exact match?
if the mark up is like this
<div class="nested-field other">
...
then you'll have to either match by the exact class or use xpath contains.