Trim function in XPath 1.0? - xpath

Is there a trim function in XPath 1.0, that can be used like this:
.//a[trim(.) = #href)]
?

There is normalize-space(), which does a little more than trimming, but which may be fine for URLs.

Related

How do I get the text from an element that has two attributes?

I have a code such as:
<out:a>
<out:Name Type="First" TypeCode="Best">JAE</out:Name>
</out:a>
When I gave the xpath expression as
//*[name()='out:Name'],
I got the result as
<out:Name Type="First" TypeCode="Best" xmlns:out3="abc" xmlns:out2="def" xmlns:out1="ghi" xmlns:out="jkl">JAE</out:Name>
I would like to get the value JAE using xpath expression. Could someone help me in that please?
Add text() on the end:
//*[name()='out:Name']/text()
Or
//out:Name/text()
It depends on the tool you use.
With java / Xpath and evaluate, your xpath expression works well:
expression=" //*[name()='out:Name']";
String value = xPath.evaluate(expression, document);
System.out.println("EVALUATE:"+value); // => EVALUATE:JAE

How to construct an xpath returning all of a set of elements

Considering theses xpath expressions :
//*[#id="searchResults"]/div[1]/div[1]/h2/span
//*[#id="searchResults"]/div[3]/div[1]/h2/span
//*[#id="searchResults"]/div[5]/div[1]/h2/span
For your info the div inside search result's class is article searchResult and the one inside article searchResult is header.
I am not sure how to construct an xpath matching all three of the above elements. Is there a tool or a how to guide for that?
Thanks
Use position function
//*[#id="searchResults"]/div[position()=1 or position()=3 or position()=5]/div[1]/h2/span
If, by 'all', you mean all div in even position index, then you can use mod operator to check :
//*[#id="searchResults"]/div[position() mod 2 = 1]/div[1]/h2/span
but if 'all' literally means all, then you don't need index to return all matched elements :
//*[#id="searchResults"]/div/div[1]/h2/span

Need help in Xpath 1.0 and 2.0 Expression using concat

I'm pulling up images dynamically based on EmployerName field using the below function.
concat("C:\Projects\GlobalResourceSet\Images\",$EmployerName,"_banner_2013.png")
It works fine.
But, If the EmployerName is wrong or its empty I should display a default image.
How can we do that using both Xpath 1.0 & 2.0
Any help will be thankful...
There's a rather bizarre solution for XPath 1.0 like this:
substring(S, 1, string-length(S) * number(C))
where S is a string and C is a boolean condition. If C is false, number(C) is 0, so nothing is output. If C is true, number(C) is 1, so the whole string is output. So the effect is "if condition C is true then output S else output nothing"; and you can combine this with another expression using the inverse condition to output different strings based on the value of the condition.
Or you could move to XPath 2.0, where life is much more boring.
In XPath 2.0, you could also do following:
concat("C:\Projects\GlobalResourceSet\Images\",
($EmployerName[. != ''], 'default')[1],
"_banner_2013.png"
)
This puts both the employer name (if set) and the "default image name" in a sequence and then selects the first of both.
Newlines added for readability, can be removed arbitrarily.
In xpath 2.0 this could work
if ($EmployerName = "") then "C:\Projects\GlobalResourceSet\Images\default.png" else concat("C:\Projects\GlobalResourceSet\Images\",$EmployerName,"_banner_2013.png")

How to use like in XPath?

I have a page that searches with filters. I have this code for example,
xmlTempResultSearch = xmlResidentListDisplay.selectNodes("//PeopleList/Row[#LastName != '"+txtSearch.value+"']");
xmlTempResultSearch.removeAll();
This selects the data that is not equal to the LastName inputted on the txtSearch textbox and then removes them from the result set so that its filtered to equal the last name on the txtSearch textbox.
My problem with this code is that it should be equal (=) to the txtSearch.value, what I want is that I want the result set LIKE the txtSearch.value. What happens on my page is that when I type 'santos' on the txtSearch textbox, its result set is all those last names with 'santos'. But when I type 'sant', nothing appears. I want the same result set with 'santos' because it all contains 'sant'
You can use all of the XPath (1.0) string functions. If you have XPath 2.0 available, then you can even use RegEx.
contains()
starts-with()
substring()
substring-before()
substring-after()
concat()
translate()
string-length()
There is no **ends-with() in XPath 1.0, but it can easily be expressed with this XPath 1.0 expression**:
substring($s, string-length($s) - string-length($t) +1) = $t
is true() exactly when the string $s ends with the string $t.
You can use start-with function and not function. Reference:
http://www.w3schools.com/xpath/xpath_functions.asp
xmlTempResultSearch = xmlResidentListDisplay.selectNodes("//PeopleList/Row[not(starts-with(#LastName,'"+ txtSearch.value +"'))]");
you can use contains() function of XPath:
xmlTempResultSearch = xmlResidentListDisplay.selectNodes("//PeopleList/Row[not(contains(#LastName,'"+txtSearch.value+"'))]");

Selenium Webdriver + Ruby regex: Can I use regex with find_element?

I am trying to click an element that changes per each order like so
edit_div_123
edit_div_124
edit_div_xxx
xxx = any three numbers
I have tried using regex like so:
#driver.find_element(:css, "#edit_order_#{\d*} > div.submit > button[name=\"commit\"]").click
#driver.find_element(:xpath, "//*[(#id = "edit_order_#{\d*}")]//button").click
Is this possible? Any other ways of doing this?
You cannot use Regexp, like the other answers have indicated.
Instead, you can use a nifty CSS Selector trick:
#driver.find_element(:css, "[id^=\"edit_order_\"] > div.submit > button[name=\"commit\"]").click
Using:
^= indicates to find the element with the value beginning with your criteria.
*= says the criteria should be found anywhere within the element's value
$= indicates to find the element with with your criteria at the end of the value.
~= allows you to find the element based on a single criteria when the actual value has multiple space-seperated list of values.
Take a look at http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ for some more info on other neat CSS tricks you should add to your utility belt!
You have no provided any html fragment that you are working on. Hence my answer is just based on the limited inputs provided your question.
I don't think WebDriver APIs support regex for locating elements. However, you can achieve what you want using just plain XPath as follows:
//*[starts-with(#id, 'edit_div_')]//button
Explanation: Above xpath will try to search all <button> nodes present under all elements whose id attribute starts with string edit_div_
In short, you can use starts-with() xpath function in order to match element with id format as edit_div_ followed by any number of characters
No, you can not.
But you should do something like this:
function hasClass(element, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
return re.test(element.className);
}
This worked for me
#driver.find_element(:xpath, "//a[contains(#href, 'person')]").click

Resources