Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
4.8k views
by The go-to Tester (158 points)

How I can use selenium webdriver to find the default text of an element? In the browser, the input field displays a default value: 'Some text', but I cannot get this text through the method getText() of this WebElement.

by The go-to Tester (181 points)
can you post HTML of that element here?

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer
The getText function will usually return text content inside the HTML tag.
Eg.
<div>Hello World</div>
Here,
driver.findElement(By,tagName("div")).getText();
Will return the text "Hello World"
 
Usually if you are working with Textbox, the getText function will return null. The Textbox values are stored in "value" attribute. So, in that case you will have to use getAttribute("value");
 
Here is the example of typical text box.
<input value="submit" />
 
Using,
driver.findElement(By.tagName("input")).getAttribute("value");
will return the text "submit"
 
Hope that helps!


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...