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.)
Related
How do I select all div's or td's with the same set of attributes.
Given some fingerprint features like className is there an easier way to select a well defined subset of element types with xpath, than to concatenate all different element types (and fingerprint attribs) with |.
I know I can write:
//div[#class="myClass"]|//td[#class="myClass"]
but since my fingerprint attributes are long and complex (this is grossly simplified) I was hoping for something in the neighbourhood of this:
//(div|td)[#class="myClass"]
but it does not seem to work out for me
I thought I had searched stackoverflow thoroughly, but I just found an answer here: XPath selecting multiple elements with predicates
and it seems like:
//*[self::div|self::td][#class="myclass"]
does the trick
(please comment here if //* makes everything 1000 times slower, or if I might have missed something important ;) )
I'm trying to get an understanding of XPath in order to parse a diffxml file. I skimmed over the w3schools site. Am I understanding these correctly?
Statement 1: /node()[1]/node()[3]
Selects the third child of the root node
Statement 2: /node()[1]/node()[1]/node()[1]
Selects the child of the first node of the root node
Statement 3: /node()[1]/node()[3]/node()[2]
Selects the second child of the third node under the root node.
Yes, you understand them correctly, but this is not how you'd use XPath. First node() can be anything, not just elements. Then the pure index is arguably the wort way of selecting things, you should really use names, and possibly predicates for filtering the node-sets.
You'll find a lot of criticism of w3schools on this site. Personally I find it a useful resource, but only when I'm trying to remind myself of something I once knew. It's not really designed for teaching yourself things from scratch, and I suggest you need a different learning strategy. Call me old-fashioned, but when I'm learning a new technology I find there's nothing better than a good book.
You've understood your examples correctly as far as I can tell. But have you understood what a "node" is? For example, do you know under what circumstances whitespace text counts as a node? The key to understanding XPath is to understand the data model, and the way in which the data model relates to the lexical (angle-bracket) form of the XML.
If a user enters a form of the word "look" such as "looked" or "looking", how can I identify it as a modified version of the verb look? I imagine others have run into and have solved this problem before ...
This is part of a fairly complicated problem called Stemming
However it's easier if you only want to take care of verb. To begin with, you can try the naive lookup table approach, since English vocabulary is not that big.
If you want something fancier, check the wiki page above.
If a regex is what your looking for something like this works look.*?\b to match look , looked and looking
Depending on your task, WordNet can be your friend for stuff like this. It's not a stemmer, but most stem words will return hits for what you're looking for It also provides synonyms and a lot of other information if you care about the concept 'look' rather than the word itself.
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']
I'm relatively new to Watir but can find no good documentation (examples) regarding how to check if an element exists. There are the API specs, of course, but these make precious little sense to me if I don't find an example.
I've tried both combinations but nothing seems to work...
if browser.image (:src "/media/images/icons/reviewertools/editreview.jpg").exists
then...
if browser.image (:src "/media/images/icons/reviewertools/editreview.jpg").exists?
then...
If anyone has a concrete suggestion as per how to implement this, please help! Thanks!
It seems you are missing a comma between parameters.
Should be
if browser.image(:src, "/media/images/icons/reviewertools/editreview.jpg").exists?
Also you can find this page useful in future to know what attributes are supported.
The code you posted should work just fine.
Edit: Oops, wrong. As Katmoon pointed out, there is a missing comma.
browser.image(:src "/media/images/icons/reviewertools/editreview.jpg").exists?
One problem you may get caught up in is if the browser variable you specified is actually an element that doesn't exist.
e.g.
b = Watir::IE.start(ipAddress)
b.frame(:name, "doesntExist).image(:src "/media/images/icons/reviewertools/editreview.jpg").exists?
The above code will throw a Watir::UnknownFrameException. You can get around this by first verifying the frame exists or by surrounding the code in a begin/rescue block.
Seems like you are using it correctly. Here is an old RDoc of Watir.
Does it not work because Watir cannot find it? Hard to tell because there is no source or link to the page that is being tested. I think that I only use image.exists?. In general, errors that come from when the image exists but is not found are:
The how is not compatible with the element type. There is a cheatsheet to help you see which object types can be found with different attributes here.
The what is not correct. You may have to play with that a little bit. Consider trying a regex string to match it such as browser.image(:src, /editreview.jpg/). As a last resort, maybe use element_by_xpath, but there are maintenance costs with that.
The location is not correct. Maybe the element is in a frame or something like that. browser.frame("detail").image(:src, /editreview.jpg/).
Try those, but please let me know what worked. One more thing, what are you checking for? If it's part of the test criteria, you can handle it that way. If you need to click on it, then forget the .exists? and just click on it. Ruby will let you know if it's not there. If you need it to be grace, learn about begin/rescue.
Good luck,
Dave