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.