views:

608

answers:

4

I want to select the values of attributes of an element. For e.g if I have an input element

<input type="text" name=myInput value="100">

I can locate it using input[name='myInput'], but how do I get the value of it using a css selector?

BTW, I am trying to do this in Selenium using css selectors

+1  A: 

You might want to explain what you're trying to do with the value. For instance, I have the following CSS to display the text of the links in the '#content' element in my print style sheet:

#content a:link:after, #content  a:visited:after {
    content: " (" attr(href) ") ";
    font-size: 90%;
}

#content a[href^="/"]:after {
    content: " (http://example.com" attr(href) ") ";
}
Joe
I have a table where in, each row has an input element with value. I have to read all those values and return as an array. Does that help?Just to add to it, I am getting that by table[class='tbl']>tbody>tr:nth-child(1)>td:nth-child(1)>inputAnd now I need to find out the value attribute of that input element
John
A: 

Your favorite JavaScript library should have some way to do this:

jQuery

$("input[name=myname]").val()

Prototype's method for CSS seloctors is $$() iirc.

For this example you could also use the native document.getElementsByName() method aswell.

Macha
John
Please add the tag Selenium to your question and re-phrase it by adding "in Selenium" somewhere in there.
slebetman
+2  A: 

If in Perl using WWW::Selenium then it's simply:

my $val = $selenium->get_value("css=input[name='myInput']");

If using another language then the Selenium library should support a get_value function/method.

slebetman
Aha! that is my problem. I wasn't using "css=". Thank you.
John
BTW, another addendum to that question - what if I have other attributes? Let us say if I have a checkbox and I want to get the id of that checkbox or its onclick attribute etc. Is there any generic way to get value of any attribute of an element?
John
If what you're after is not handled by the basic Selenium API then there's always `get_eval()` that allows you to write javascript code and return anything you want: innerHTML, javascript variable, element attributes etc.
slebetman
Example: `my $txt = $selenium->get_eval('document.getElementById("foo").title');`
slebetman