How can I use xpath not operator in APPIUM - xpath

I tested these two XPath expressions:
//*[not(#class='android.view.ViewGroup[2]')]
and
//*[not(contains(#class,'android.view.ViewGroup[2]'))]
But that syntax doesn't work. I want to filter unavailable days with not or another operator. But I can't. Thanks for help

Try the following:
//*[not(#class='android.view.ViewGroup')][2]
or, an easier version:
//*[#class!='android.view.ViewGroup'][2]
Both select the second matching element with the given condition.

Related

How to cement two xpath expressions into one?

Can't get any idea to cement the two expressions into one xpath. They both fall under the same class "pagination". I need to use that in a loop. I tried separately like this:
//div[#class='pagination']//a/#href
//div[#class='pagination']//a[contains(#class,'next')]/#href
Elements for the expression:
<div class="pagination"><p><span>Showing</span>1-30
of 483<span>results</span></p><ul><li><span class="disabled">1</span></li><li>2</li><li>3</li><li>4</li><li>5</li><li>Next</li></ul></div>
There are many ways you can combine two XPath expressions. One way, for example, is to use the union operator "|". Whether that's the right operator to use depends on what you want to achieve. Unfortunately you forget to tell us what you want to achieve, so that might not be the right operator for your purposes.

Is there a short and elegant way to write an XPath 1.0 expression to get all HREF values containing at least one of many search values?

I was just wondering if there is a shorter way of writing an XPath query to find all HREF values containing at least one of many search values?
What I currently have is the following:
//a[contains(#href, 'value1') or contains(#href, 'value2')]
But it seems quite ugly, especially if I were to have more values.
First of all, in many cases you have to live with the "ugliness" or long-windedness of expressions if only XPath 1.0 is at your disposal. Elegance is something introduced with version 2.0, I'd daresay.
But there might be ways to improve your expression: Is there a regularity to the href attributes you'd like to find? For instance, if it is sufficient as a rule to say that the said href attribute values must start with "value", then the expression could be
//a[starts-with(#href,'value')]
I know that "value1" and "value2" are most probably not your actual attribute values but there might be something else that uniquely identifies the group of a elements you're after. Post your HTML input if this is something you want us to help you with.
Personally, I do not find your expression ugly. There is just one or operator and the expression is quite short and readable. I take
if I were to have more values.
to mean that currently, there are only two attribute values you are interested in and that your question therefore is a theoretical one.
In case you're using XPath 2 and would like to have exact matches instead of also matches only containing part of a search value, you can shorten with
//a[#href = ('value1', 'value2')]
For contains() this syntax wouldn't work as the second argument of contains() is only allowed to be 0 or 1 value.
In XPath 2 you could also use
//a[some $s in ('value1', 'value2') satisfies contains(#href, $s)]
or
//a[matches(#href, "value1|value2")]

PowerCenter - Regular Expressions

I got the following regular expression:
=([^"]*)
Basically I want to extract a value between = and " (example: "City=Paris", output "Paris"). But for some reason this expression wont work in PowerCenter.
You got any idea how to implement it?
Thank you
Thomas
You can try this
REG_EXTRACT('"City=Paris"','(.*\w+=)(\w+)(".*)',2)
This basically breaks up the string in three groups of characters and returns the second group.

Ruby Regular Expressions: Matching if substring doesn't exist

I'm having an issue trying to capture a group on a string:
"type=gist\nYou need to gist this though\nbecause its awesome\nright now\n</code></p>\n\n<script src=\"https://gist.github.com/3931634.js\"> </script>\n\n\n<p><code>Not code</code></p>\n"
My regex currently looks like this:
/<code>([\s\S]*)<\/code>/
My goal is to get everything in between the code brackets. Unfortunately, it's matching up to the 2nd closing code bracket Is there a way to match everything inside the code brackets up until the first occurrence of ending code bracket?
All repetition quantifiers in regular expressions are greedy by default (matching as many characters as possible). Make the * ungreedy, like this:
/<code>([\s\S]*?)<\/code>/
But please consider using a DOM parser instead. Regex is just not the right tool to parse HTML.
And I just learned that for going through multiple parts, the
String.scan( /<code>(.*?)<\/code>/ ){
puts $1
}
is a very nice way of going through all occurences of code - but yes, getting a proper parser is better...

xpath to check '#' present

I want to write xpath to check node contain '#'
<node1>
<node11>Some text</node11>
<node11>#2o11 PickMe</node12>
</node1>
I want to write xpath like "//node11[contains(,'#\d+')]". Whats correct way to check #
The correct XPath expression is:
//node11[contains(., '#')]
In your XML, the closing tag of the second subnote should be </node11> instead of </node12>.
If you are using xpath 2.0 you should be able to use something like:
"//node11[matches(.,'#\d+')]"
However, if you aren't using 2.0 you won't have regex support directly. If you are using 1.0 then you won't be able to match using \d+. But this will work:
"//node11[contains(.,'#')]"
Or even:
"//node11[starts-with(.,'#')]"
Use:
/*/node11[contains(., '#')]
Note: It is recommended to avoid using the // pseudo-operator because this most often leads to very slow evaluation of the XPath expression.

Resources