开发者

How to extract attribute values using css selectors?

开发者 https://www.devze.com 2022-12-14 20:12 出处:网络
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 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 开发者_JAVA技巧of it using a css selector?

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


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) ") ";
}


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.


You could use the following:

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


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.


In Java Selenium WebDriver there are 2 ways to do this

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号