Hello Isha, you can checkout this code snippet for extracting text from a web element and saving it as a text or doc file:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ExtractText {
static WebDriver driver;
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "chrome_driver_path");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.edureka.co/devops");
String output = driver.findElement(By.xpath("/html/body/div[1]/div[5]/div/div/div[1]/div[2]/div[1]/div")).getText();
File DestFile= new File("extractedFilePath");
FileUtils.writeStringToFile(DestFile, output);
driver.close();
}
}
For further understanding, you can refer to the Selenium Course.