views:

66

answers:

2

Hi,

I'm trying to extract all name values in input fields using selenium and perl. Part of the value, enough to identify it, is known, the rest is unknown:

This xpath works in finding all relevant matches:

//tr/td//input[contains(@name,'partofname')]

So, in perl:

my $xpath = qq(//tr/td//input[contains(\@name,'partofname')]);
my $count = $sel->get_xpath_count($xpath);

Fine, $count gives a suitable count of matches.

However, how to extract the value of the @name attribute for each individual matches?

I understand the principle is to construct a loop:

foreach my $row (1 .. $count) {
#extract here
};

However, I can't seem to construct an xpath expression which will work to find each $row that the expression matched. So I think it's the correct xpath expression to get each individual match that I need help with.

Any pointers appreciated

+1  A: 

Try //tr/td/descendant::input[contains(@name,'partofname')][1]

Replace 1 with your counter. If that doesn't could you add some HTML to your question so I can perhaps suggest a better XPath?

Dave Hunt
Hi - thanks for the suggestion but this produced: Invalid xpath [2]: //tr/td/descendant::input[contains(for each node.The html is just a table, with rows and a few table data entries for each row - one of which contains input attributes including name, id etc.
CColin
A snippet of your HTML would still be useful.
Dave Hunt
What minimum sized snippet would be useful? Table is rather big, as is each row - would one row with several <td> tags, one of which contains the sought after attributes, do?
CColin
It's really just for the structure of your HTML, so just a couple of dummy rows would be fine. I just want to make sure I can test whatever solution I might be able to provide.
Dave Hunt
A: 

However, how to extract the value of the @name attribute for each individual matches?

In case this XPath expression selects all elements with the wanted attribute (as stated in the qustion):

//tr/td//input[contains(@name,'partofname')] 

then this XPath expression selects all name attributes:

//tr/td//input[contains(@name,'partofname')]/@name

Then you'll only need in your programming language to iterate through this final node-set.

Dimitre Novatchev
Hi - this doesn't work since the expression contains *all* matches - and so does the attribute matching. The question is how do you find an individual match in order that you can iterate
CColin
@CColin: Why dont you "iterate" on all selected nodes? These are exactly the nodes you want.
Dimitre Novatchev
You can't iterate on all selected nodes without using the correct syntax to access each element - what is the correct syntax to access each individual node? Simply appending [$row] in the hope that it will return each element in the query doesn't work. So how to iterate on all selected nodes is the point of the question...xpath count just returns a count - there are no results in the variable to iterate over.
CColin
So, this is a Selenium question -- not an XPath question at all.
Dimitre Novatchev
It is both an XPath and Selenium question.
Dave Hunt