如何用Python-Selenium每10秒刷新页面检查Jenkins任务成功/失败状态?
Hey there! Let's fix up your script to properly monitor your Jenkins task's completion status. First, let's break down the issues with your current code, then walk through an optimized version.
Jenkins Task Status Monitoring with Python-Selenium
Core Issues in Your Current Script
- Broken Logic Flow: Your
elseclause only ties to the secondif failurecheck. That means if the script detectsSUCCESS, it will still trigger theelseblock and refresh the page—definitely not what you want. - No Continuous Monitoring: The script only runs one check; if it doesn't find a result, it refreshes once and stops. It won't keep checking until the Jenkins task finishes.
- No Timeout Safeguard: Since your task runs 15-30 minutes, without a timeout the script could loop infinitely if something goes wrong.
- Refresh Interval Mismatch: You wanted a 10-second refresh, but the script uses
time.sleep(5).
Optimized Full Script
import time import re from selenium import webdriver # Initialize your browser driver (adjust for Chrome/Firefox/etc.) driver = webdriver.Chrome() driver.get("YOUR_JENKINS_TASK_PAGE_URL") # Set max wait time (30 mins = 1800 secs, add a small buffer) max_wait_seconds = 1800 start_timestamp = time.time() # Loop until we get a result or hit the timeout while True: # Check if we've exceeded the max wait time elapsed_time = time.time() - start_timestamp if elapsed_time > max_wait_seconds: print("Timeout reached: Jenkins task ran longer than 30 minutes") break page_source = driver.page_source # Search for both statuses success_found = re.search(r'Finished: SUCCESS', page_source) failure_found = re.search(r'Finished: FAILURE', page_source) if success_found: print('✅ Task completed successfully: SUCCESS') break elif failure_found: print('❌ Task failed: FAILURE') break else: print(f"Still waiting... Elapsed time: {int(elapsed_time)}s. Refreshing in 10s.") time.sleep(10) driver.refresh() # Clean up: close the browser to avoid leftover processes driver.quit()
Key Improvements Explained
- Correct Logic Structure: Uses
if-elif-elseto ensure we only refresh when neither status is found. - Continuous Monitoring: The
while Trueloop keeps checking until we get a success/failure or hit the timeout. - Timeout Protection: Prevents infinite loops by stopping the script after 30 minutes.
- Matching Refresh Interval: Updated to
time.sleep(10)to align with your requirement. - Resource Cleanup:
driver.quit()ensures the browser closes properly after the script finishes.
Bonus: More Efficient Dynamic Waiting
If your Jenkins page loads status updates dynamically, using Selenium's explicit waits is more efficient than checking the full page source repeatedly. Here's how you can adjust the loop:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # Replace the loop logic above with this while True: elapsed_time = time.time() - start_timestamp if elapsed_time > max_wait_seconds: print("Timeout reached: Jenkins task ran longer than 30 minutes") break try: # Wait up to 10 seconds for the "Finished:" status element to appear status_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'Finished:')]")) ) if "SUCCESS" in status_element.text: print('✅ Task completed successfully: SUCCESS') else: print('❌ Task failed: FAILURE') break except: # If the status doesn't appear in 10s, refresh and try again print(f"Still waiting... Elapsed time: {int(elapsed_time)}s. Refreshing now.") driver.refresh()
This approach targets the exact status element instead of scanning the entire page source, making it faster and more reliable.
内容的提问来源于stack exchange,提问作者Sidharth Gokul
相关产品推荐
相关产品推荐

