SELENIUM : How to get text from Text box using Selenium
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
To get the text from a text box using Selenium WebDriver in Java, you can use the getAttribute() method of the WebElement class.
The getAttribute() method allows you to retrieve the value of the specified attribute of the element.
Here's an example code snippet that demonstrates how to get the text from a text box:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetTextFromTextBoxExample {
public static void main(String[] args) {
// Create an instance of the ChromeDriver
WebDriver driver = new ChromeDriver();
// Navigate to the desired URL
driver.get("[ Ссылка ]");
// Find the text box element
WebElement textBox = driver.findElement(By.id("myTextBox"));
// Get the text from the text box
String text = textBox.getAttribute("value");
// Print the text
System.out.println("Text in the text box: " + text);
// Close the browser
driver.quit();
}
}
In this example, we first set the path of the chromedriver executable using the System.setProperty() method.
Then, we create an instance of the ChromeDriver and navigate to the desired URL using the get() method.
Next, we find the text box element using the findElement() method and the By class.
We then use the getAttribute() method to get the value of the value attribute of the text box element, which contains the text in the text box.
Finally, we print the text using the println() method and close the browser using the quit() method of the WebDriver interface.
Note that if you want to get the text that is displayed on the web page but not necessarily entered by the user into the text box, you can use the getText() method instead of the getAttribute() method. For example:
String text = textBox.getText();
This code will get the text that is displayed on the web page and associated with the text box element.
![](https://i.ytimg.com/vi/p0KtMoyTCfg/mqdefault.jpg)