Created
August 20, 2025 16:39
-
-
Save tshrinivasan/94d63b958020ae0c4110536991a8e162 to your computer and use it in GitHub Desktop.
get_kindle_book_screenshot_chrome.py
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.chrome.service import Service | |
import time | |
import os | |
import platform | |
import shutil | |
import tempfile | |
def copy_chrome_profile(): | |
"""Copy Chrome profile to a temporary directory""" | |
system = platform.system() | |
if system == "Linux": | |
source_profile = os.path.expanduser("~/.config/google-chrome/Default") | |
elif system == "Darwin": | |
source_profile = os.path.expanduser("~/Library/Application Support/Google/Chrome/Default") | |
elif system == "Windows": | |
source_profile = os.path.join(os.environ['LOCALAPPDATA'], 'Google', 'Chrome', 'User Data', 'Default') | |
else: | |
raise Exception("Unsupported OS") | |
# Create temporary directory for profile copy | |
temp_profile = tempfile.mkdtemp(prefix="chrome_profile_") | |
# Copy profile files (excluding large cache files) | |
if os.path.exists(source_profile): | |
for item in os.listdir(source_profile): | |
if item not in ['Cache', 'Code Cache', 'GPUCache']: # Skip large cache directories | |
source_item = os.path.join(source_profile, item) | |
dest_item = os.path.join(temp_profile, item) | |
if os.path.isdir(source_item): | |
shutil.copytree(source_item, dest_item) | |
else: | |
shutil.copy2(source_item, dest_item) | |
return temp_profile | |
# Copy profile to temporary location | |
temp_profile_dir = copy_chrome_profile() | |
print(f"Using temporary profile: {temp_profile_dir}") | |
# Chrome options with temporary profile | |
options = Options() | |
options.add_argument(f"--user-data-dir={temp_profile_dir}") | |
options.add_argument("--profile-directory=.") # Use current directory of the temp folder | |
options.add_argument("--start-maximized") | |
options.add_argument("--disable-notifications") | |
options.add_experimental_option("excludeSwitches", ["enable-automation"]) | |
options.add_experimental_option('useAutomationExtension', False) | |
service = Service("./chromedriver") | |
try: | |
driver = webdriver.Chrome(service=service, options=options) | |
print("Chrome driver initialized successfully with copied profile") | |
url = "https://read.amazon.com/?<id>" | |
driver.get(url) | |
print("Page loaded") | |
time.sleep(30) | |
url = "https://read.amazon.com/?<id>" | |
driver.get(url) | |
print("Page loaded") | |
# Wait for page to load | |
WebDriverWait(driver, 20).until( | |
EC.presence_of_element_located((By.TAG_NAME, "body")) | |
) | |
time.sleep(3) | |
# Create screenshots directory | |
if not os.path.exists('screenshots'): | |
os.makedirs('screenshots') | |
for i in range(1,1000): | |
action = webdriver.ActionChains(driver) | |
element = driver.find_element(By.CSS_SELECTOR,".kr-chevron-container-right") # or your another selector here | |
action.move_to_element(element) | |
action.perform() | |
driver.implicitly_wait(1) | |
driver.find_element(By.ID, "kr-chevron-right").click() | |
driver.implicitly_wait(3) | |
file_name = str(i).zfill(4) | |
driver.save_screenshot('screenshots/book_' + file_name +'.png') | |
time.sleep(3) | |
except Exception as e: | |
print(f"Error: {str(e)}") | |
finally: | |
try: | |
driver.quit() | |
print("Browser closed") | |
# Clean up temporary profile | |
shutil.rmtree(temp_profile_dir, ignore_errors=True) | |
print("Temporary profile cleaned up") | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment