File size: 11,142 Bytes
314a377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
"""
WordPress Elementor Design Agent (Basic Implementation)
"""

import time
import logging
import argparse
import os
from typing import Optional
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

class ElementorAgent:
    """
    Agent for automating Elementor page builder tasks in WordPress (Basic Implementation).
    """
    
    def __init__(self, url: str, username: str, password: str, post_id: Optional[int] = None, headless: bool = False):
        """
        Initialize the Elementor agent.
        """
        self.url = url
        self.username = username
        self.password = password
        self.post_id = post_id
        self.headless = headless
        self.driver: Optional[webdriver.Chrome] = None
        
    def start(self) -> bool:
        """
        Start the browser and log in to WordPress (Placeholder Implementation).
        """
        logger.info(f"Starting Elementor Agent (basic) for {self.url}")
        
        chrome_options = Options()
        if self.headless:
            chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")
        
        try:
            self.driver = webdriver.Chrome(
                service=Service(ChromeDriverManager().install()),
                options=chrome_options
            )
            
            # Login to WordPress
            logger.info(f"Navigating to {self.url}")
            self.driver.get(self.url)
            
            # Wait for login form
            username_field = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.ID, "user_login"))
            )
            password_field = self.driver.find_element(By.ID, "user_pass")
            
            # Fill login form
            username_field.send_keys(self.username)
            password_field.send_keys(self.password)
            
            # Submit form
            password_field.submit()
            
            # Wait for admin dashboard
            WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.ID, "wpadminbar"))
            )
            
            logger.info("Successfully logged in to WordPress")
            
            # If post_id is provided, navigate to Elementor editor
            if self.post_id:
                self.navigate_to_elementor()
                
            return True
            
        except Exception as e:
            logger.error(f"Error starting browser: {e}")
            if self.driver:
                self.driver.quit()
                self.driver = None
            return False
    
    def navigate_to_elementor(self) -> bool:
        """
        Navigate to the Elementor editor for the specified post.
        """
        if not self.driver or not self.post_id:
            return False
            
        try:
            editor_url = f"{self.url}post.php?post={self.post_id}&action=elementor"
            logger.info(f"Navigating to Elementor editor: {editor_url}")
            self.driver.get(editor_url)
            
            # Wait for Elementor to load
            WebDriverWait(self.driver, 30).until(
                EC.presence_of_element_located((By.ID, "elementor-preview-iframe"))
            )
            
            logger.info("Successfully loaded Elementor editor")
            return True
            
        except Exception as e:
            logger.error(f"Error navigating to Elementor: {e}")
            return False
    
    def close(self) -> None:
        """
        Close the browser and clean up.
        """
        if self.driver:
            logger.info("Closing browser")
            self.driver.quit()
            self.driver = None
            
    def add_widget(self, widget_type: str) -> bool:
        """
        Add a widget to the Elementor page.
        """
        if not self.driver:
            return False
            
        try:
            # Switch to Elementor iframe
            preview_iframe = self.driver.find_element(By.ID, "elementor-preview-iframe")
            self.driver.switch_to.frame(preview_iframe)
            
            # Click on add widget button
            add_section = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CLASS_NAME, "elementor-add-section-button"))
            )
            add_section.click()
            
            # Switch back to main frame to interact with widget panel
            self.driver.switch_to.default_content()
            
            # Search for widget
            search_input = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, ".elementor-panel-search-input"))
            )
            search_input.send_keys(widget_type)
            
            # Find and click on widget
            widget_element = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, f"//div[contains(@class, 'elementor-element-title-wrapper') and contains(text(), '{widget_type}')]"))
            )
            widget_element.click()
            
            logger.info(f"Added widget: {widget_type}")
            return True
            
        except Exception as e:
            logger.error(f"Error adding widget: {e}")
            return False

    def edit_text(self, element_selector: str, text: str) -> bool:
        """
        Edit text content of an Elementor element.
        """
        if not self.driver:
            return False
            
        try:
            # Switch to Elementor iframe
            preview_iframe = self.driver.find_element(By.ID, "elementor-preview-iframe")
            self.driver.switch_to.frame(preview_iframe)
            
            # Find and click the element to edit
            element = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, element_selector))
            )
            element.click()
            
            # Switch back to main frame to interact with panel
            self.driver.switch_to.default_content()
            
            # Find text editor input
            text_editor = WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, ".elementor-control-content [data-setting='editor']"))
            )
            
            # Clear existing text and set new text
            self.driver.execute_script("arguments[0].innerHTML = arguments[1]", text_editor, text)
            
            logger.info(f"Edited text for selector: {element_selector}")
            return True
            
        except Exception as e:
            logger.error(f"Error editing text: {e}")
            return False

    def save_changes(self) -> bool:
        """
        Save changes made in the Elementor editor.
        """
        if not self.driver:
            return False
            
        try:
            # Make sure we're in the main frame
            self.driver.switch_to.default_content()
            
            # Click the save button
            save_button = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, ".elementor-button.elementor-button-success"))
            )
            save_button.click()
            
            # Wait for save to complete
            WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR, ".elementor-button-success .elementor-state-icon"))
            )
            
            logger.info("Successfully saved changes")
            return True
            
        except Exception as e:
            logger.error(f"Error saving changes: {e}")
            return False

    def take_screenshot(self, filename: str = "elementor_screenshot.png") -> bool:
        """
        Take a screenshot of the current state of the Elementor editor.
        """
        if not self.driver:
            return False
            
        try:
            # Make sure we're in the main frame
            self.driver.switch_to.default_content()
            
            # Take screenshot
            self.driver.save_screenshot(filename)
            logger.info(f"Screenshot saved: {filename}")
            return True
            
        except Exception as e:
            logger.error(f"Error taking screenshot: {e}")
            return False


def parse_arguments():
    """Parse command line arguments."""
    parser = argparse.ArgumentParser(description='WordPress Elementor Design Agent (Basic)')
    parser.add_argument('--url', default=os.environ.get('WP_ADMIN_URL', 'http://localhost/wp-admin/'),
                        help='WordPress admin URL')
    parser.add_argument('--username', default=os.environ.get('WP_USERNAME', 'admin'),
                        help='WordPress username')
    parser.add_argument('--password', default=os.environ.get('WP_PASSWORD', ''),
                        help='WordPress password')
    parser.add_argument('--post-id', type=int, default=os.environ.get('WP_POST_ID', 1),
                        help='Post ID to edit with Elementor')
    parser.add_argument('--headless', action='store_true',
                        help='Run browser in headless mode')
    parser.add_argument('--screenshot', action='store_true',
                        help='Take a screenshot after loading Elementor')
    parser.add_argument('--screenshot-path', default='elementor_editor.png',
                        help='Path to save the screenshot')
    parser.add_argument('--wait-time', type=int, default=5,
                        help='Time to wait after loading Elementor (seconds)')

    return parser.parse_arguments()


def main():
    """Main function to run the script."""
    args = parse_arguments()

    agent = ElementorAgent(
        url=args.url,
        username=args.username,
        password=args.password,
        post_id=args.post_id,
        headless=args.headless
    )

    if agent.start():
        try:
            logger.info(f"Elementor Agent (basic) started successfully for post ID {args.post_id}")

            # Take a screenshot if requested
            if args.screenshot:
                agent.take_screenshot(args.screenshot_path)

            # Wait the specified time
            if args.wait_time > 0:
                logger.info(f"Waiting for {args.wait_time} seconds...")
                time.sleep(args.wait_time)

        finally:
            agent.close()
    else:
        logger.error("Failed to start Elementor Agent (basic)")


if __name__ == "__main__":
    main()