To write data obtained using Selenium WebDriver to a file, you can follow these general steps:
- Extract the data you want to write from the web page using Selenium WebDriver’s methods.
- Example is given below
- Store the extracted data in a variable or a list.
- Open a file in the desired mode (e.g., write mode, append mode) using Python’s built-in open() function.
- Write the data to the file using the file object’s write() method.
- Close the file to ensure the data is saved correctly.
# Example to get the table element
driver.find_element(By.XPATH, "//table")
Here’s an example that demonstrates how to write scraped data to a file using Selenium WebDriver and Python:
from selenium import webdriver
# Create a WebDriver instance (e.g., ChromeDriver)
driver = webdriver.Chrome()
# Navigate to the desired webpage
driver.get('https://codetryout.com')
# Extract the data you want (example: page title)
title = driver.title
# Close the WebDriver
driver.quit()
# Write the data to a file
file_path = 'webdriver-output.txt' # Replace with your desired file path
with open(file_path, 'w') as file:
file.write(title)
print("Data written to file successfully.")
In this example, the script navigates to https://codetryout.com/ using Selenium WebDriver, extracts the page title, closes the WebDriver, and then writes the extracted title to a file named “output.txt” using the write() method.
You can customize the code according to your specific needs and extract and write multiple data points by appending them to the file using successive write() calls or iterating through a list of data.
Related: Take a ScreenShot using Webdriver
Writing and Reading
Python’s open() function for file operations is preferred due to its simplicity, platform consistency, resource management, error handling, and library compatibility. The built-in with statement further ensures proper file closure and promotes clean and readable code.
You can use the following snippet to continue with your Python code and retrieve the data written.
# Open a file in read mode
with open("webdriver-output.txt", "r") as file:
content = file.read()
print(content)
# File is automatically closed when the 'with' block is exited