xPath last select element - ruby-on-rails-3

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.

Related

How to write xpath for following example?

For example, I have div tag that has two attributes.
class='hello#123' text='321#he#321llo#321'
<div> class='hello#123' text='321#he#321llo#321'></div>
Here, I want to write xpath for both class and text attributes but numbers may change dynamically. ie., "hello#123" may become "345" when we reload. "321#he#321llo#321" may become "567#he#456llo#321".
Note: Need to write xpath in single line not separately.
Assuming that you have the (corrected) two-attribute-HTML
<div class='hello#123' text='321#he#321llo#321'>...</div>
you can select it using the following, for example:
Using the contains() function
//div[contains(#class,'hello') and contains(#text,'#he#')]
This is quite specific and only applicable if the "hello" is always split in the same way
Using the translate() function to mask everything except the chars for "hello"
//div[translate(#class,'#0123456789','')='hello' and translate(#text,'#0123456789','')='hello']
This removes all # chars and digits and checks if the remaining string is "hello"
I guess combining these two approaches you will be able to create your own XPath expression fitting your needs. The patterns you provided were not fully clear, so this may only approach a good enough solution.

Build XPath using wildcard for changing strings

I am trying to build an XPath for a property that is constantly changing. The number prefix is bound to change sometimes.
Original:
//*[#id="MainContent_DXEditor3_I"]
which I can query using
$x('//*[#id="MainContent_DXEditor3_I"]
Intended use: I would like to build the string to handle any number in the sub-string. Example: if the property changes to 'MainContent_DXEditor33_I' or 'MainContent_DXEditor8_IXYZ' - I still want to be able to find the element without having to rebuild
You can try to relax the predicate by using starts-with() :
//*[#starts-with(#id, "MainContent_DXEditor")]
You should try to identify a unique parent of the element or save xpath as a string that contains a variable.
These are the 2 possible solutions.
A general selector will return multiple elements, if you identify a unique parent then you are closer and after that you can select any first, second.. last if you have a list.

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

selecting first span value from a group in selenium

I would want to select the first instance of an element in a page where many number of such elements are present with 'ID' which will not be same always.
for example, visit, http://www.sbobet.com/euro which lists lot of sports and odds, where I want to click on the first odds.
and the html structure would be like this,
I want to click on this first span value and proceed with some test case.
Any help on how to achieve this ?
There could be two approaches two the problem:
1. If you are sure you will always need only the first instance:
driver.FindElementsByClassName("OddsR")[0];
If not, then you have collection of elemets and you can access an of those
2. Also, you can first identify any closest enclosing div and then you can use the same snippet as above:
driver.FindElementsByClassName("OddsR")[0];
This one is a better approach if page is a bit dynamic in nature
Use #class attribute. If OddsR class you are intrested in is the 1st one on the page then just use Driver.FindElement(By.ClassName("OddsR")). Webdriver will pick the 1st occurence (no matter if there are more)
Have checked your link and I agree with alecxe, you should probably start with div. But i would suggest a simpler selector :
css = "div.MarketBd span.OddsR"
The above selector will always point to the first span of "OddsR" class within div of "MarketBd" class.
Thanks for the response.
I am finally able to click on the element, by this XPATH,
"//span[#class='OddsR']"
This clicks on the first occurrence of 'OddsR' values, without giving any index.

elastic-search on/and nesting possible?

Can and/or be nested in filters?
I want to filter something like this... a=1 AND ( d=NULL OR d>5 )
Can anyone help?
You can use a Lucene query string like this in elastic search:
http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html
and here is a reference to how you can use () for grouping ... one thing to note is the I found prefixing group statements with the + must have symbol returns more accurate results.
http://lucene.apache.org/java/3_2_0/queryparsersyntax.html
grouping is at the end.
... oh and if you going to use greater than RangeQuery can cover this case by
setting either the upper or lower term to null.
J.
This may be a bit late, but if anyone else is looking for this, I found that search word AND filters are easy to add. Just use more words in the query and separate them with space.
Scala Example:
val queryString = client.prepareSearch().setQuery(QueryBuilders.matchQuery("json.path.to.node", "sky car")).setSize(MAX_RESULTS)
val response = queryString.execute().actionGet()
Now response will have all results which containt both sky AND car