Simplifying an xPath query - xpath

Simplify the following xPath so that it is as small as possible but still identifies the element in question
/html/body/div[#id='vwd4_page']/div[#id='vwd4_content']/div[#id='Layout']/div[#id='FooterAction']/div[3]/form[#id='searchform']/span/input[#id='searchfield']

Can you use this?
//input[#id='searchfield']

Related

How to elegantly find multiple siblings in xpath

I'm hoping there's a better way of doing this than what I've already done.
I've got an xpath statement that like this
'//div[#class="findthis"]/a[1]/text()|//div[#class="findthis"]/a[2]/text()|...//div[#class="findthis"]/a[10]/text()'
doing it this way feels very dumb, is there a more elegant way to do this?
This should do the trick:
//div[#class="findthis"]//a//text()
This will find all text descendands of all a attributes
If you want the text node children of the first ten a elements, that would be
//div[#class="findthis"]/a[position() <= 10]/text()
(But you've accepted an answer which does something quite different to your original expression, so it's not really clear what you are looking for.)

Standard process for creating complex xpath in protractor

I am looking for standard ways to arrive at complex xpath expressions in protractor.
For e.g. I have a complex xpath as follows:
(//*[contains(#class,'day')][normalize-space(text())='2'])[1]
Here I have to get first access to elements matching xpath
//*[contains(#class,'day')][normalize-space(text())='2']
and then pick the first from the matching ones. Any pointers?
Protractor in its documentation clearly describes any process for creating xpaths:
http://www.protractortest.org/#/style-guide [section Locator strategies].
Firstly, you shouldn't use XPath except as a last resort. I second the recommendation by #Kacper to read the style guide he posted.
However, if you're dead set on using XPath, (sometimes it is unavoidable), you can pick the first element that matches like so:
element.all(by.xpath("//*[contains(#class,'day')][normalize-space(text())='2']")).first();

HTMLAgilityPack xpath

I have hit a wall with this one, please could someone help me out?
From the URL below I am looking to get to the inner text of
A2A
The XPath syntax I am using doesn't return any data:
.//table[#class='table_dati']//tbody[#class='constituents']//tr//td[#class='name']//a
The URL is
http://www.borsaitaliana.it/borsa/azioni/ftse-mib/lista.html?lang=en&page=1
Thanks in advance,
Grant
How about //tbody[#class='constituents']//td[#class='name']/a? This should work pretty well, actually.
Your XPath starts with ., so it is relative to the context node. But you haven't told us anything about the context. Maybe you want to omit the initial . and make it "absolute":
//table[#class='table_dati']/tbody[#class='constituents']/tr/td[#class='name']/a
I would also change the // to / wherever you're looking for a direct child (not descendant in general) relationship.
From my experience, HTMLAgilityPack doesn't play nice with the tbody tag. I just follow up the table with the tr td to find the right cell, skipping tbody altogether.

Xpath best practices

I have a readonly xml file and I have a set of xpath values.
I need to create a function which would take in the xpath and return the value(s) corresponding to the xpath.
I am a little confused regarding what would be the best way to proceed. The options I am thinking are using the regular XPathDocument/Navigator/Iterator classes or using LINQ to xml.
The function I am trying to implement is:
T GetString(string inputXpath) where T could be bool/string/array etc.
Can someone help?Also, this function is going to be called all across the application, so performance might be a consideration.
Thank you!
-Agent
What you want to write will just return:
XpathNavigator.Evaluate(inputXpath);
Obviously, T must be just... object :)
Read the XpathNavigator.Evaluate() documentation here.

Is there an XPath equivilent for Linq to XML?

I have been using Linq to XML for a few hours and while it seems lovely and powerful when it comes to loops and complex selections, it doesn't seem so good for situations where I just want to select a single node value which XPath seems to be good at.
I may be missing something obvious here but is there a way to use XPath and Linq to XML together without having to parse the document twice?
You can still use XPath, with the XPathEvaluate, XPathSelectElement and XPathSelectElements extension methods. You can also call CreateNavigator to create an XPathNavigator.

Resources