Methods in Webelement GetText () and getattribute (java.lang.String name) __web

Source: Internet
Author: User

GetText (): Gets the visible inline text for the element.

such as the link in the CSDN home page <a class= "left" target= "_blank" href= "http://www.csdn.net" onclick= logclickcount (this,285); " > Home </a>.

Pass

Driver = new Firefoxdriver ();
url = "http://www.csdn.net/";
Driver.get (URL);
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);
String Text = driver.findelement (By.xpath ("//div[@class = ' csdn_pub_nav_bg ']/div[1]/a[1]")). GetText ();

Can get the text "homepage".

For elements of the input type on the page (Input,textarea, etc.), you cannot get the corresponding text through GetText (). such as the CSDN home page in the search box in the default "search" word. Its label is <input id= "srch1" class= "search" type= "text" onblur= "if (this.value==") this.value= ' searches '; This.style.color= ' #999 '; return true; "onfocus=" if (this.value== ' search ') this.value= '; this.style.color= ' #333 '; return true; "value=" searches "name=" passwordtwo "style=" Color:rgb (153, 153, 153); " />

by string errorvalue = Driver.findelement (By.xpath ("//input[@class = ' search ')"). GetText (); the resulting value is null. The getattribute () should be used at this time.

GetAttribute (String name): Gets the value of the property named name in the element.

by string value = Driver.findelement (By.xpath ("//input[@class = ' search ')"). GetAttribute ("value"); To get the value "search".

by String type = Driver.findelement (By.xpath ("//input[@class = ' search ')"). GetAttribute ("type"); the corresponding value "text" is obtained.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.