Can anyone help me to read a label text from selenium webdriver
The for attribute value and label text are entirely different
Can anyone help me to read a label text from selenium webdriver
The for attribute value and label text are entirely different
Assuming the label looks in HTML something like this:
<span id="spannyspan" title="the Title of SPAN">The Text</span>
then the WebElement will be best approached like this:
WebDriver driver = new FirefoxDriver();
WebElement theSpan = driver.findElement(By.id("spannyspan"));
String title = theSpan.getAttribute("title");
String label = theSpan.getText();
System.out.println(title); // will return "the Title of SPAN"
System.out.println(label); // will return "The Text"
// both without apostrophes ofcourse
If this does not help please provide sample HTML of label which you are trying to fetch
Suppose there is a label contain text "hello":
<label>hello</label>
then what you have to do is: # first find this element using xpath or css locator and store that into WebElement object:
WebElement label = driver.findElement(By.xpath("//label[contains(text(),'hello')]");
# After that using getText() method you can get the text of the label element.getText() method return value in string so,store the value in the string variable
String lbltext = label.getText();
# After that if you want to print the value then
System.out.println("text = "+lbltext);