13

I want to select the values of attributes of an element. 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

5
  • It would be helpful if you gave some hint as to the technology you are using to "select" and "locate". XPath in XSLT? JavaScript using DOM? JavaScript with jQuery? There is so much to guess amongst. Commented Dec 29, 2009 at 1:34
  • Sorry, this is in Selenium. I am trying to use CSS Selectors (instead of XPath) to get the value of an input element. Commented Dec 29, 2009 at 1:39
  • Apparently another person asked a similar question, but again that person wasn't trying to extract the value of the input element he was after stackoverflow.com/questions/1909584/xpath-to-css-selector Commented Dec 29, 2009 at 1:48
  • In your response below you mentioned you're using Selenium. Please change the title of your question to "How to extract attribute values with Selenium" to get better responses. Commented Dec 29, 2009 at 1:52
  • What language are using Selenium in. Perl? Commented Dec 29, 2009 at 1:53

5 Answers 5

9

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) ") ";
}
Sign up to request clarification or add additional context in comments.

1 Comment

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)>input And now I need to find out the value attribute of that input element
2

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.

4 Comments

Aha! that is my problem. I wasn't using "css=". Thank you.
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?
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.
Example: my $txt = $selenium->get_eval('document.getElementById("foo").title');
1

You could use the following:

 WebDriver driver=new FirefoxDriver();
    String nameAttribute=driver.findElement(By.cssSelector("Your css selector")).getAttribute("name")

Comments

0

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.

2 Comments

I should've added this before. I am trying to do this in Selenium & I am trying to extract value using CSS locators that selenium understands. So I guess, I have to look at what JS library Selenium uses for CSS evaluation.
Please add the tag Selenium to your question and re-phrase it by adding "in Selenium" somewhere in there.
0

In Java Selenium WebDriver there are 2 ways to do this

driver.findElement(By.name("myInput")).getText() driver.findElement(By.name("myInput")).getAttribute(value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.