- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I get the current contents of an element in webdriver?
We can get the current contents of an element in Selenium webdriver. For the elements having tagname as <input> we have to use the getAttribute() method and pass the value parameter as an argument to that method to obtain the current contents.
For the elements without an input tag we have to use the getText() method to obtain the current contents. First of all we have to identify the element with the help of the locators.
Let us try to get the content Selenium inside the edit box and its above text content – You are browsing the best resource for Online Education.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ElementContent{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); // identify elements WebElement t = driver.findElement(By.cssSelector("h4")); WebElement l = driver.findElement(By.id("gsc-i-id1")); // getAttribute() with value for content in edit box System.out.println("Content in edit box: " + l.getAttribute("value")); //getText() to get content System.out.println("Content in element: " + t.getText()); driver.close(); } }
Output
- Related Articles
- How do I get current URL in Selenium Webdriver 2 Python?
- How can I get Webdriver Session ID in Selenium?
- How to get an attribute value of an element in Selenium Webdriver?
- How to get all descendants of an element using webdriver?
- How can I get the ID of an DOM element using jQuery?
- How can I get the current screen orientation in Android?
- How can I get the current week using Python?
- How can I get the current time and date in Kotlin?
- How can I get the current Android SDK version programmatically?
- How can I list the contents of a directory in Python?
- How do I find an element that contains specific text in Selenium Webdriver?
- How can I get the current Android SDK version programmatically using Kotlin?
- How do I set the Selenium webdriver get timeout?
- How do I find an element that contains specific text in Selenium WebDriver (Python)?
- How can I apply an offset on the current time in Python?

Advertisements