File size: 3,638 Bytes
509a107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
try:
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    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.service import Service
    from selenium.webdriver.chrome.options import Options
    from webdriver_manager.chrome import ChromeDriverManager
    SELENIUM_AVAILABLE = True
except ImportError:
    SELENIUM_AVAILABLE = False
    print("Selenium not installed - demo automation disabled")

try:
    import pyautogui
    PYAUTOGUI_AVAILABLE = True
except ImportError:
    PYAUTOGUI_AVAILABLE = False
    print("PyAutoGUI not installed - automation features limited")

import time
import asyncio

class BrowserAutomation:
    def __init__(self):
        self.driver = None
        if PYAUTOGUI_AVAILABLE:
            pyautogui.FAILSAFE = True
            pyautogui.PAUSE = 0.5
    
    def initialize_driver(self):
        if not SELENIUM_AVAILABLE:
            print("Selenium not available - cannot initialize driver")
            return
            
        try:
            chrome_options = Options()
            chrome_options.add_argument("--no-sandbox")
            chrome_options.add_argument("--disable-dev-shm-usage")
            chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
            chrome_options.add_experimental_option('useAutomationExtension', False)
            
            service = Service(ChromeDriverManager().install())
            self.driver = webdriver.Chrome(service=service, options=chrome_options)
            
            self.driver.set_window_size(1280, 720)
            self.driver.set_window_position(100, 100)
            
        except Exception as e:
            print(f"Driver initialization error: {e}")
            self.driver = None
    
    async def run_demo(self, url: str, text_to_type: str):
        loop = asyncio.get_event_loop()
        await loop.run_in_executor(None, self._run_demo_sync, url, text_to_type)
    
    def _run_demo_sync(self, url: str, text_to_type: str):
        if not SELENIUM_AVAILABLE:
            print(f"Demo mode: Would open {url} and type '{text_to_type}'")
            time.sleep(2)
            return
            
        try:
            if self.driver is None:
                self.initialize_driver()
            
            if self.driver:
                self.driver.get(url)
                
                time.sleep(2)
                
                try:
                    search_box = self.driver.find_element(By.TAG_NAME, "input")
                    search_box.click()
                    search_box.send_keys(text_to_type)
                except:
                    body = self.driver.find_element(By.TAG_NAME, "body")
                    body.click()
                    body.send_keys(text_to_type)
                
                time.sleep(1)
                
                if PYAUTOGUI_AVAILABLE:
                    original_window = pyautogui.getActiveWindow()
                    if original_window:
                        original_window.activate()
                
                time.sleep(5)
                
                self.driver.quit()
                self.driver = None
                
        except Exception as e:
            print(f"Demo execution error: {e}")
            if self.driver:
                self.driver.quit()
                self.driver = None
    
    def cleanup(self):
        if self.driver:
            self.driver.quit()
            self.driver = None