iT邦幫忙

0

Python selenium driver 多進行續

  • 分享至 

  • xImage

各位午安
因小弟是新手Python
目前再嘗試多進行續,程式碼如下
一開始使用quit()去進行關閉,卻只會關閉一個視窗,改用.close()和.join()卻會出現ValueError: Pool is still running 的錯誤
想像各位大師求救是哪裡有問題,拜託了

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import sqlite3
import xml.etree.ElementTree as ET
#讀取xml檔
import threading
#儲存driver開啟時的變數資料
from multiprocessing.pool import ThreadPool
#多行程

def read_sql(input_value):

conn = sqlite3.connect(input_value)
cursor = conn.execute("SELECT id, name, phone, email  from Usertable") 
return cursor

def read_xlm(input_value):

tree = ET.parse(input_value)
root = tree.getroot()
obj=root.findall('A')
return obj    

def read_browser(input_value):

browser = getattr(threadLocal, 'browser', None)
if browser is None:
    headers = {
        'User-Agent': 'Googlebot',
        'From': 'YOUR EMAIL ADDRESS'
    }
    option = webdriver.ChromeOptions()
    option.add_argument('disable-gpu')
    option.add_argument('blink-settings=imagesEnabled=false')
    browser=webdriver.Chrome(chrome_options=option)
    browser.implicitly_wait(5)

    setattr(threadLocal, 'browser', browser)
url=input_value
browser.get(url)
return browser

def read_program(input_value):

xlm_obj=read_xlm('filename.xml')
browser=read_browser('https://www.clue.com.tw/webinars/LandingPage/Get/mD6hD')

try:
    username=browser.find_element_by_name(xlm_obj[0][0].text)
    username.send_keys(input_value[1])
    
    userFull_cphone=browser.find_element_by_name(xlm_obj[0][1].text)
    userFull_cphone.send_keys(input_value[2])

    usermail=browser.find_element_by_name(xlm_obj[0][2].text)
    usermail.send_keys(input_value[3])

    browser.find_element_by_id('btn_submit').click()
    result = EC.alert_is_present()(browser)

    if result :
        alert = browser.switch_to.alert 
        result=alert.text
        alert.accept()
    else:
        try:
            title = browser.title
            if title == "SaveSuccess - TransferMVC":
                result="成功輸入"     
        except:
            try:
                title = browser.title
                if title == "SaveSuccess - TransferMVC":
                    result="成功輸入"
            except:
                result="其他錯誤"
except:
        browser.quit()               
        result="異常中斷"
        print(result)

   
csv_result=(input_value[1],input_value[2],input_value[3],result)
return csv_result ,browser    
    

if name == 'main':

threadLocal = threading.local()
sql_cursor=read_sql('Users(alert).db')

program= ThreadPool(2).map(read_program,sql_cursor)

ThreadPool(2).close()
ThreadPool(2).join()

#program[0][1].quit()
#直接使用只會關閉一個視窗

資料庫的部分
https://ithelp.ithome.com.tw/upload/images/20210721/20133374XoJf6qmJOP.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答