On Oct 14, 3:19 pm, Matt <[email protected]> wrote: > Excellent thank you! that worked perfectly! > For future reference is there a way to do this with xpath? >
I'm sure it could be done, but the question is WHY? xpath code is ugly, hard to read and understand, prone to typo errors, fragile and brittle, and generally slower to execute. What possible reason could you have to want to use xpath unless as a means of last resort? Besides which, most times you are trying to interact with one of many similar fields, you are either doing the same thing to all of them, or wanting to interact with a specific one of the many fields, usually based on it's proximity to some other object that is unique. In the first case, getting a collection, and using the .each method to feed a loop is far superior to addressing each one individually by xpath (see Z's answer in this thread). In the second case it is generally far clearer to deal with code which clearly expresses that intent, than gobblygook xpath. I mean seriously wouldn't you rather have something like this in your script than xpath junk? browser.row(:text, /Æther Shanties/).link(:text, 'Add to Cart').click Isn't it far far easier to tell what that code is doing than trying to make sense of something expressed in xpath to do the same thing? That command by the way, (in case you were unsure) would look for a row (in a table) that contained the text "Æther Shanties" (an album by steampunk band Abney Park) somewhere in the row (there could be other text around it, but that specific text is what is searched for thanks to the magic of a 'regular expression'), it then looks in the same row for a link with the text "Add to Cart" and executes the .click method on that link. It thus follows the same logic that a human would for determining which of many potential 'add to cart' links to click. It would also work no matter what row of the table the content appeared on, so if new data in the system caused the (presumed) search results to change slightly, it would still work provided the target link was there on the page. And that last is something that would often break using a specific xpath that is looking for the thing to be found on the 4th row of the table or somesuch. To my mind anyway when you are asking 'could I do this via xpath' it's as if we've offered you a modern scientific calculator and you are asking 'but could I do this with log tables and a slide rule'.. and hence the 'uh sure, but why would you want to?' response. -- Before posting, please read http://watir.com/support. In short: search before you ask, be nice. [email protected] http://groups.google.com/group/watir-general [email protected]
