How to extract attribute values ​​using css selectors?

I want to select attribute values ​​for an element. for example if i have an input element

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

I can find it using input[name='myInput'] , but how to get its value using the css selector?

By the way, I'm trying to do this in Selenium using css selectors

+4
source share
5 answers

If Perl uses WWW :: Selenium, then it's simple:

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

When using another language, the Selenium library must support the get_value function / method.

+2
source

You might want to explain what you are trying to do with the value. For example, I have the following CSS to display the link text in the "#content" element in my print stylesheet:

 #content a:link:after, #content a:visited:after { content: " (" attr(href) ") "; font-size: 90%; } #content a[href^="/"]:after { content: " (http://example.com" attr(href) ") "; } 
+8
source

You can use the following:

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

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

JQuery

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

The prototype method for seloctors CSS is $$ () iirc.

In this example, you can also use the native document.getElementsByName () method.

0
source

There are 2 ways to do this in Java Selenium WebDriver

driver.findElement (By.name ("myInput")). GetText () driver.findElement (By.name ("myInput")). GetAttribute (value)

0
source

All Articles