- Selenium WebDriver 3 Practical Guide
- Unmesh Gundecha Satya Avasarala
- 162字
- 2021-08-13 16:08:09
The getText() method
The getText method can be called from all the WebElements. It will return visible text if the element contains any text on it, otherwise it will return nothing. The API syntax for the getText() method is as follows:
java.lang.String getText()
There is no input parameter for the preceding method, but it returns the visible innerText string of the WebElement if anything is available, otherwise it will return an empty string.
The following is the code to get the text present on the Site notice element present on the example application Homepage:
@Test
public void elementGetTextExample() {
WebElement siteNotice = driver.findElement(By
.className("global-site-notice"));
System.out.println("Complete text is: "
+ siteNotice.getText());
}
The preceding code uses the getText() method to fetch the text present on the Site notice element, which returns the following:
Complete text is: This is a demo store. Any orders placed through this store will not be honored or fulfilled.
推薦閱讀
- 社交網絡對齊
- 網絡協議工程
- Oracle SOA Suite 11g Performance Tuning Cookbook
- Web Application Development with R Using Shiny
- HCNA網絡技術
- WordPress 5 Complete
- Spring Cloud微服務架構進階
- 互聯網基礎資源技術與應用發展態勢(2021—2023)
- Mastering JavaFX 10
- 中國互聯網發展報告2018
- Microservice Patterns and Best Practices
- OMNeT++與網絡仿真
- 新手易學:新手學淘寶開店
- Learning Node.js Development
- 圖神經網絡前沿