Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in DevOps and Agile by (19.7k points)

Both are used to get the WebElement value in between tags? Kindly update whether my assumption is right. If wrong please elaborate it.

1 Answer

0 votes
by (62.9k points)
edited by

getAttribute() -> It fetch the text that containing one of any attributes in the HTML tag . Suppose there is HTML tag like 

<input name="Name Locator" value="selenium">Hello</input>

now getAttribute() fetch the data of the attribute of value which is "Selenium"

 Returns: The attribute's current value or null if the value is not set.

driver.findElement(By.name("Name Locator")).getAttribute("value")  // The field value is //retrieved by the getAttribute("value") Selenium WebDriver predefined method and //assigned to the String object.

getText() -> delivers the innerText of a WebElement. Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

Returns: The innerText of this element.

driver.findElement(By.name("Name Locator")).getText();

Hello will be shown 

I hope this will be helpful. For more related queries on Selenium, visit the Selenium tutorial page or check more Selenium interview questions.

To learn in-depth about Selenium, sign up for an industry based Selenium training.

Browse Categories

...