File size: 9,302 Bytes
9287a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 这个项目是用来实现自动开机,需要两个号
# 当 A即将运行完之后,调用selenium自动开启B账号的实例。B即将运行完之后,运行A账号实例
# 参考该网站: https://many-oryx-physically.ngrok-free.app 基本上全天运行SD
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浏览器为无界面模式
chrome_options.add_argument('--headless')
 
 
 
#下面的两行代码一般是不需要的,至少经过我的测试在windows上是不需要的。但经过我的测试在Ubuntu上如果不加的话等下就会报错WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser,所以要加上
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")
        
            #driver.implicitly_wait(0.5)
        
            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}")
        # Function to perform the login and task creation
    
    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")
            # 首先清除由于浏览器打开已有的cookies
            driver.delete_all_cookies()
            with open('cookies.txt','r') as f:
            # 使用json读取cookies 注意读取的是文件 所以用load而不是loads
                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") #FASOXO改成你的用户名
        
        def fill_form(image_address, startup_file):
            try:
                # 使用显式等待等待输入框出现,这里等待10秒,可以根据实际情况调整
                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") #填写表单
        # Wait for the new task button to appear
        def click_close_button():
            try:
                # 使用显式等待等待按钮出现,这里等待10秒,可以根据实际情况调整
                close_button = WebDriverWait(driver, 10).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR, 'button.el-button.el-button--primary.el-button--small'))
                )
        
                # 使用 ActionChains 模拟鼠标移动到按钮上并点击
                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:
                # 使用显式等待等待按钮出现,这里等待10秒,可以根据实际情况调整
                create_task_button = WebDriverWait(driver, 10).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR, 'button.el-button.submit-btn'))
                )
        
                # 点击按钮
                        # 使用 ActionChains 模拟鼠标移动到按钮上并点击
                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")
                # 使用显式等待等待链接出现,这里等待10秒,可以根据实际情况调整
                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]

                # 去掉token部分
                new_path = parsed_url.path
                if "?" in new_path:
                    new_path = new_path.split("?")[0]

                # 构建新的URL,省略端口号和token部分
                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:
            # Click the new task button
                new_task_button.click()
        #run()
        
    # Create a webdriver session
        click_close_button()
        click_create_task_button()
        click_create_task_button()
        click_create_task_button() #连续点三次创建任务,防止出意外
        #!sleep 60
        #click_online_inference_link()
    # Main loop with a 30-minute interval
    try:
        perform_operations(driver)

        print("完成")
        time.sleep(200)  # Sleep for 30 minutes (1800 seconds)

    finally:
        # Close the browser outside the loop
        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:
        #main() #测试用
        print("成功")
        #print(response.json())
        time.sleep(5)
        detect()
    else:
        print("failed")
        main()
        detect()

while True:
    main()