小鬼 发表于 2022-1-20 10:31:20

Python selenium跟多线程使用

from selenium.webdriver.common.by import By
from time import sleep, time, strftime, localtime
from selenium import webdriver
import threading
import time
global wd,user,pwd,login,chaoshi,js,js2,ID

def denglu(lxnum):

    wd = webdriver.Chrome(r'D:\python\chromedriver_win32\chromedriver.exe')
    wd.get("www.ddddddd")
    sleep(5)
    wd.implicitly_wait(10)# 设置最大等待时长
    chaoshi=wd.find_element(By.CLASS_NAME,'amsdialog_bconfirm')
    if chaoshi is not None:
      chaoshi.click()
    wd.find_element(By.ID,'dologin').click()
    wd.switch_to.frame('loginIframe')
    wd.find_element(By.ID,'switcher_plogin').click()
    user = wd.find_element(By.ID,'u')
    pwd = wd.find_element(By.ID,'p')
    user.send_keys('user')
    pwd.send_keys('passwd')
    login = wd.find_element(By.ID,'login_button')
    login.click()
    sleep(3)
    wd.switch_to.default_content()
    sleep(2)
    ifwd.find_element(By.CSS_SELECTOR, '') is not None:
    # wd.execute_script("$(lx).click()",button)
    # print(button.get_attribute('outerHTML'))
      js = 'setTimeout(function () {{lx({});$("#min").val(2);$("#max").val(8);$("#cj").click();$("#qrcj").click(); }},1)'.format(
            lxnum)
      js2 = 'setTimeout(function () {$("#qrcj").click(); },200)'
      wd.execute_script(js)
      wd.execute_script(js2)
    return
def main():
    ID=(1,2,5,6,7,9,14,16)
    start_threads = []# 用来存放执行read函数线程的列表
    for i in ID :# 创建1个线程用于t(),并添加到start_threads列表
      t = threading.Thread(target=denglu,args=)# 执行的函数如果需要传递参数,threading.Thread(target=函数名,args=(参数,逗号隔开))
      start_threads.append(t)
    for i in range(1,8):
      start_threads.start()
if __name__ == '__main__':
    main()




运行到登录之后的地方,所有浏览器都闪退,不知道是代码有问题还是,有大佬帮忙看看么

suchocolate 发表于 2022-1-22 09:34:06

本帖最后由 suchocolate 于 2022-1-22 09:38 编辑

没有join,改一下:
    for i in ID:
      t = threading.Thread(target=denglu, args=)
      start_threads.append(t)
      t.start()
    for i in start_threads:
      i.join()

小鬼 发表于 2022-1-25 11:22:20

suchocolate 发表于 2022-1-22 09:34
没有join,改一下:

好的,谢谢,我试试
页: [1]
查看完整版本: Python selenium跟多线程使用