openclaw / script.py
laura15824899's picture
Update script.py
a3b820d verified
Raw History Blame Contribute Delete
2.29 kB
from cloakbrowser import launch_persistent_context
import time
import random
PROFILE_DIR = "/app/zo_profile"
HEADLESS = True # True:无头运行,False:有界面(按回车关闭)
TARGET_URL = "https://666878.zo.computer/_boot"
MAX_RETRIES = 3
RETRY_DELAY_BASE = 2
PAGE_LOAD_TIMEOUT = 60000
SCREENSHOT_ON_SUCCESS = False
def main():
context = None
page = None
try:
print("正在启动浏览器...")
context = launch_persistent_context(
PROFILE_DIR,
headless=HEADLESS,
args=['--no-sandbox']
)
page = context.new_page()
for attempt in range(1, MAX_RETRIES + 1):
try:
print(f"尝试访问 ({attempt}/{MAX_RETRIES}): {TARGET_URL}")
page.goto(TARGET_URL, wait_until="load", timeout=PAGE_LOAD_TIMEOUT)
page.wait_for_selector("body", timeout=5000)
print(f"页面加载成功,当前 URL: {page.url}")
print(f"页面标题: {page.title()}")
if SCREENSHOT_ON_SUCCESS:
screenshot_path = f"screenshot_attempt_{attempt}.png"
page.screenshot(path=screenshot_path)
print(f"已保存截图: {screenshot_path}")
break
except Exception as e:
print(f"第 {attempt} 次访问失败: {e}")
if attempt == MAX_RETRIES:
print("所有重试均失败,脚本终止。")
return
delay = RETRY_DELAY_BASE ** attempt + random.uniform(0, 1)
print(f"等待 {delay:.1f} 秒后重试...")
time.sleep(delay)
if HEADLESS:
print("Headless模式,等待 10 秒后自动关闭...")
time.sleep(10)
else:
input("非Headless模式,按回车键关闭浏览器...")
except Exception as e:
print(f"脚本运行发生未捕获异常: {e}")
finally:
if context:
print("正在关闭浏览器...")
try:
context.close()
except Exception as e:
print(f"关闭浏览器时出错: {e}")
print("脚本结束。")
if __name__ == "__main__":
main()