Created
May 5, 2023 14:38
-
-
Save Ishmam156/f0db9387b0cbdafb45467a7bedb8d5bd to your computer and use it in GitHub Desktop.
Simple script that uses selenium to replicate browser behavior to save data in an excel format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install the required packages as below | |
# On Windows: pip install selenium webdriver_manager pandas | |
# On Mac: pip3 install selenium webdriver_manager pandas | |
import datetime | |
import pandas as pd | |
import time | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from webdriver_manager.chrome import ChromeDriverManager | |
# Generate today's date in a format that the frontend website will accept | |
def today(): | |
today = datetime.datetime.today() | |
# format the date as "MM/DD/YYYY" | |
date_string = today.strftime("%m/%d/%Y") | |
return date_string | |
# Initialize a new Chrome browser instance | |
driver = webdriver.Chrome(ChromeDriverManager().install()) | |
# Navigate to localhost:3000 and input the login details | |
driver.get("http://localhost:3000") | |
time.sleep(1) | |
email_input = driver.find_element(By.XPATH, "//input[@name='email']") | |
email_input.send_keys("ishmam@something.com") | |
password_input = driver.find_element(By.XPATH, "//input[@name='password']") | |
password_input.send_keys("something") | |
time.sleep(1) | |
submit_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']") | |
submit_button.click() | |
time.sleep(1) | |
# In the redirected webpage, click on a specific button based on the button text | |
transformed_data_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Transformed data')]") | |
transformed_data_button.click() | |
time.sleep(1) | |
# In the redirected webpage, enter the date in the date input | |
date_picker = driver.find_element(By.TAG_NAME, "input") | |
date_picker.click() | |
date_picker.send_keys(today()) | |
# Iterate through all the rows of table data and store it | |
all_rows = driver.find_elements(By.TAG_NAME, 'tr') | |
all_data = [] | |
for row in all_rows: | |
split_data = row.text.split(' ') | |
if len(split_data) == 5: | |
all_data.append(split_data) | |
# Create a Pandas dataframe from the data | |
df = pd.DataFrame(all_data, columns=['#', 'Name', 'Sales Quantity', 'Sale Locations', 'Total Revenue']) | |
# Save the dataframe as an Excel file | |
df.to_excel('output.xlsx', index=False) | |
# close the browser | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment