memex-in commited on
Commit
6b90b86
·
verified ·
1 Parent(s): 290a6c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -3,36 +3,43 @@ import time
3
  import streamlit as st
4
  from selenium import webdriver
5
  from selenium.webdriver.chrome.options import Options
 
 
 
6
  import tempfile
7
 
8
- # Function to capture a screenshot of a webpage
9
  def capture_screenshot(url, output_path):
10
  chrome_options = Options()
11
- chrome_options.add_argument('--headless')
12
- chrome_options.add_argument('--disable-gpu')
13
- chrome_options.add_argument('--no-sandbox')
14
- chrome_options.add_argument('--window-size=1920,1080')
15
 
16
  driver = webdriver.Chrome(options=chrome_options)
17
  try:
18
  driver.get(url)
 
 
 
 
 
19
  driver.save_screenshot(output_path)
20
  finally:
21
  driver.quit()
22
 
23
  # Streamlit UI
24
- st.title("Timed Web Screenshot Viewer")
25
 
26
  url = st.text_input("Enter a website URL:", "https://example.com")
27
  wait_time = st.number_input("Time to wait before taking screenshot (in seconds):", min_value=1, max_value=30, value=5, step=1)
28
 
29
  if st.button("Generate Screenshot"):
30
- screenshot_path = None # Initialize to ensure it's defined
31
  try:
32
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
33
  screenshot_path = tmp_file.name
34
 
35
- # Show a countdown
36
  with st.empty():
37
  for i in range(wait_time, 0, -1):
38
  st.info(f"Waiting... {i} seconds left")
 
3
  import streamlit as st
4
  from selenium import webdriver
5
  from selenium.webdriver.chrome.options import Options
6
+ from selenium.webdriver.support.ui import WebDriverWait
7
+ from selenium.webdriver.support import expected_conditions as EC
8
+ from selenium.webdriver.common.by import By
9
  import tempfile
10
 
11
+ # Function to capture a screenshot of a webpage after redirection
12
  def capture_screenshot(url, output_path):
13
  chrome_options = Options()
14
+ chrome_options.add_argument('--headless')
15
+ chrome_options.add_argument('--disable-gpu')
16
+ chrome_options.add_argument('--no-sandbox')
17
+ chrome_options.add_argument('--window-size=1920,1080')
18
 
19
  driver = webdriver.Chrome(options=chrome_options)
20
  try:
21
  driver.get(url)
22
+
23
+ # Wait until the page is loaded and title stabilizes (handles JavaScript-based redirects)
24
+ WebDriverWait(driver, 15).until(lambda d: d.execute_script('return document.readyState') == 'complete')
25
+ time.sleep(2) # Small extra wait to let JS redirects finish if any
26
+
27
  driver.save_screenshot(output_path)
28
  finally:
29
  driver.quit()
30
 
31
  # Streamlit UI
32
+ st.title("Timed Web Screenshot Viewer (with Redirect Support)")
33
 
34
  url = st.text_input("Enter a website URL:", "https://example.com")
35
  wait_time = st.number_input("Time to wait before taking screenshot (in seconds):", min_value=1, max_value=30, value=5, step=1)
36
 
37
  if st.button("Generate Screenshot"):
38
+ screenshot_path = None
39
  try:
40
  with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
41
  screenshot_path = tmp_file.name
42
 
 
43
  with st.empty():
44
  for i in range(wait_time, 0, -1):
45
  st.info(f"Waiting... {i} seconds left")