The below code snippet will help in extracting text from a webpage using Selenium and saving it as a text 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();
}
}
If you want to unleash your potential in this competitive field, please visit the Selenium Certification Training page for more information, where you can find the Selenium Tutorial and Selenium Interview Questions FAQ's and answers as well.
For more updates on the latest courses stay tuned to HKR Trainings.