python多线程执行任务Threading

2022-08-24 12:22:35 浏览数 (1)

代码如下:

代码语言:javascript复制
# coding=utf-8
"""
    作者:gaojs
    功能:
    新增功能:
    日期:2022/6/9 18:21
"""
from selenium import webdriver
from selenium.webdriver.common.by import By
import threading
import time
import sys


browsers = []
threads = []
num = int(sys.argv[1])
i = 0

for i in range(0, num):

    # chrome_options = webdriver.ChromeOptions()
    # chrome_options.add_argument('--ignore-certificate-errors')
    # chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
    browser = webdriver.Chrome()
    browser.implicitly_wait(3)
    browsers.append(browser)


def saml_request(browser):
    global i
    while True:
        try:
            browser.get("https://192.168.120.222/prx/000/http/localhost/login/sp1/default/login/index.html")
            # 点击高级选项
            if i == 0:
                browser.find_element_by_xpath('//button[@id="details-button"]').click()
                # 点击详情连接,跳出隐私连接
                browser.find_element_by_xpath('//a[@id="proceed-link"]').click()
            # 点击下拉框
            browser.find_element_by_xpath('//button[@class="btn dropdown-toggle btn-default"]').click()
            # 选择method
            browser.find_element_by_xpath('//a[@role="option"] //span[contains(text(),"sm1")]').click()
            # 点击sign in 按钮
            browser.find_element_by_xpath('//button[@id="saml_login"]').click()
            # 点击高级选项
            if i == 0:
                browser.find_element_by_xpath('//button[@id="details-button"]').click()
                # 点击详情连接,跳出隐私连接
                browser.find_element_by_xpath('//a[@id="proceed-link"]').click()
            # 跳转idp,等待时间
            time.sleep(2)
            # 登录操作
            browser.find_element_by_name("uname").send_keys("u1")
            browser.find_element_by_name("pwd").send_keys("u1")
            browser.find_element_by_name("submitbutton").click()
            time.sleep(2)
            # 退出登录
            browser.find_element_by_xpath("//a[text()='Logout']").click()
            print("%s %d" % (time.ctime(), i))
            i  = 1
        except:
            pass


for i in range(0, num):
    thread = threading.Thread(target=saml_request, args=(browsers[i],))
    # 开启新线程
    thread.start()
    # 添加线程到线程列表
    threads.append(thread)

# 等待所有线程完成
for i in range(0, num):
    threads[i].join()
print('退出主线程')

0 人点赞