|
|
|
|
|
|
|
from selenium import webdriver |
|
from selenium.webdriver.common.by import By |
|
from selenium.webdriver.support.ui import WebDriverWait |
|
from selenium.webdriver.support import expected_conditions as EC |
|
import time |
|
import json |
|
from selenium.webdriver.common.action_chains import ActionChains |
|
from selenium.webdriver.common.keys import Keys |
|
from urllib.parse import urlparse, urlunparse |
|
from selenium.webdriver.chrome.options import Options |
|
from selenium.webdriver.common.action_chains import ActionChains |
|
from selenium.webdriver.chrome.options import Options |
|
|
|
|
|
chrome_options=Options() |
|
|
|
|
|
|
|
chrome_options.add_argument('--headless') |
|
|
|
|
|
|
|
|
|
chrome_options.add_argument('--no-sandbox') |
|
chrome_options.add_argument('--disable-dev-shm-usage') |
|
|
|
|
|
is_login = True |
|
def main(): |
|
driver = webdriver.Chrome(options=chrome_options) |
|
def login(): |
|
try: |
|
driver.get("https://openi.pcl.ac.cn/user/login") |
|
|
|
|
|
|
|
text_box = driver.find_element(by=By.NAME, value="user_name") |
|
password = driver.find_element(by=By.ID, value="input_password") |
|
submit_button = driver.find_element(by=By.ID, value="submitId") |
|
|
|
text_box.send_keys("FA***") |
|
password.send_keys("NSw///") |
|
submit_button.click() |
|
|
|
driver.add_cookie({"name": "foo", "value": "bar"}) |
|
|
|
print(driver.get_cookie("foo")) |
|
except Exception as e: |
|
print(f"An error occurred: {e}") |
|
|
|
|
|
if is_login == True: |
|
print("Logging in...") |
|
login() |
|
else: |
|
print("跳过登录") |
|
def perform_operations(driver): |
|
|
|
|
|
if is_login == False: |
|
driver.get("https://openi.pcl.ac.cn/index.html") |
|
|
|
driver.delete_all_cookies() |
|
with open('cookies.txt','r') as f: |
|
|
|
cookies_list = json.load(f) |
|
for cookie in cookies_list: |
|
driver.add_cookie(cookie) |
|
else: |
|
pass |
|
|
|
driver.get("https://openi.pcl.ac.cn/FASOXO/sd2/grampus/onlineinfer/create") |
|
|
|
def fill_form(image_address, startup_file): |
|
try: |
|
|
|
image_input = WebDriverWait(driver, 10).until( |
|
EC.presence_of_element_located((By.CSS_SELECTOR, 'input[placeholder="选择镜像或输入镜像地址"]')) |
|
) |
|
startup_input = WebDriverWait(driver, 10).until( |
|
EC.presence_of_element_located((By.CSS_SELECTOR, 'input[placeholder="请输入启动文件"]')) |
|
) |
|
|
|
|
|
|
|
image_input.send_keys(image_address) |
|
startup_input.send_keys(startup_file) |
|
|
|
|
|
|
|
except Exception as e: |
|
print(f"填写表单过程中发生错误: {str(e)}") |
|
|
|
fill_form("192.168.204.22:5000/default-workspace/99280a9940ae44ca8f5892134386fddb/image:sd_v10", "app.py") |
|
|
|
def click_close_button(): |
|
try: |
|
|
|
close_button = WebDriverWait(driver, 10).until( |
|
EC.presence_of_element_located((By.CSS_SELECTOR, 'button.el-button.el-button--primary.el-button--small')) |
|
) |
|
|
|
|
|
actions = ActionChains(driver) |
|
actions.move_to_element(close_button).click().perform() |
|
|
|
|
|
|
|
except Exception as e: |
|
print(f"点击关闭按钮过程中发生错误: {str(e)}") |
|
def click_create_task_button(): |
|
try: |
|
|
|
create_task_button = WebDriverWait(driver, 10).until( |
|
EC.presence_of_element_located((By.CSS_SELECTOR, 'button.el-button.submit-btn')) |
|
) |
|
|
|
|
|
|
|
actions = ActionChains(driver) |
|
actions.move_to_element(create_task_button).click().perform() |
|
|
|
|
|
print("创建任务按钮已按下") |
|
except Exception as e: |
|
print(f"点击按钮过程中发生错误: {str(e)}") |
|
def click_online_inference_link(): |
|
global current_url |
|
try: |
|
driver.get("https://openi.pcl.ac.cn/FASOXO/sd2/grampus/onlineinfer") |
|
|
|
online_inference_link = WebDriverWait(driver, 10).until( |
|
EC.element_to_be_clickable((By.XPATH, "//div[@class='op-wrap']//a[contains(text(), '在线推理')]")) |
|
) |
|
|
|
|
|
actions = ActionChains(driver) |
|
actions.move_to_element(online_inference_link).click().perform() |
|
|
|
current_window_handle = driver.current_window_handle |
|
|
|
|
|
online_inference_link.send_keys(Keys.CONTROL + Keys.RETURN) |
|
|
|
|
|
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)) |
|
|
|
|
|
all_window_handles = driver.window_handles |
|
|
|
|
|
for window_handle in all_window_handles: |
|
if window_handle != current_window_handle: |
|
driver.switch_to.window(window_handle) |
|
break |
|
|
|
current_url = driver.current_url |
|
|
|
|
|
parsed_url = urlparse(current_url) |
|
netloc_without_port = parsed_url.netloc.split(":")[0] |
|
|
|
|
|
new_path = parsed_url.path |
|
if "?" in new_path: |
|
new_path = new_path.split("?")[0] |
|
|
|
|
|
new_url = urlunparse((parsed_url.scheme, netloc_without_port, new_path, |
|
parsed_url.params, parsed_url.query, parsed_url.fragment)) |
|
print(f"当前浏览的网页URL: {new_url}") |
|
except Exception as e: |
|
print(f"点击链接过程中发生错误: {str(e)}") |
|
|
|
def run(): |
|
if "is-disabled" in new_task_button.get_attribute("class"): |
|
print("不可用,restart") |
|
driver.quit() |
|
main() |
|
perform_operations(driver) |
|
else: |
|
|
|
new_task_button.click() |
|
|
|
|
|
|
|
click_close_button() |
|
click_create_task_button() |
|
click_create_task_button() |
|
click_create_task_button() |
|
|
|
|
|
|
|
try: |
|
perform_operations(driver) |
|
|
|
print("完成") |
|
time.sleep(200) |
|
|
|
finally: |
|
|
|
driver.quit() |
|
|
|
|
|
|
|
|
|
import requests |
|
def detect(): |
|
url = 'https://mouse-glowing-husky.ngrok-free.app/internal/ping' |
|
headers = {'accept': 'application/json'} |
|
|
|
response = requests.get(url, headers=headers) |
|
|
|
if response.status_code == 200: |
|
|
|
print("成功") |
|
|
|
time.sleep(5) |
|
detect() |
|
else: |
|
print("failed") |
|
main() |
|
detect() |
|
|
|
while True: |
|
main() |