我有兩個硒 python 代碼。我正在使用子行程腳本運行它們:
import subprocess
subprocess.run("python script1.py & python script2.py", cwd=r'C:\Users\David\Desktop\Selenium', shell=True)
當我運行它們時,它們不會并行運行,而是按順序運行。有誰知道我可以如何調整這個腳本,以便它們同時運行(并行)?
以下是腳本 1 和 2 的代碼:
腳本1.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options=Options()
driver=webdriver.Chrome(options=options)
params={'behavior':'allow','downloadPath':r'C:\Users\David\Downloads'}
driver.execute_cdp_cmd('Page.setDownloadBehavior',params)
driver.get("https://data.gov.uk/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/input"))).send_keys("Forestry Statistics 2018: Recreation")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/div/button"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/form/main/div/div[2]/div[2]/div[2]/h2/a"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div/div/div/section/table/tbody/tr[2]/td[1]/a"))).click()
腳本2.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options=Options()
driver=webdriver.Chrome(options=options)
params={'behavior':'allow','downloadPath':r'C:\Users\David\Downloads'}
driver.execute_cdp_cmd('Page.setDownloadBehavior',params)
driver.get("https://data.gov.uk/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/input"))).send_keys("NI 179 - Value for money")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div[2]/form/div/div/div/button"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/form/main/div/div[2]/div[2]/div[2]/h2/a"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/main/div/div/div/section/table/tbody/tr/td[1]/a"))).click()
uj5u.com熱心網友回復:
我不確定究竟是如何subprocess.run作業的,但我已經嘗試過subprocess.Popen并且運行良好。
試試這個:
import subprocess
firstScript = subprocess.Popen(["python", "C:/Users/David/Desktop/Selenium/script1.py"], shell=True)
secondScript = subprocess.Popen(["python", "C:/Users/David/Desktop/Selenium/script2.py"], shell=True)
firstScript.wait()
secondScript.wait()
uj5u.com熱心網友回復:
嘗試使用pytest-xdist
點安裝 pytest-xdist
pytest -n 自動
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464531.html
標籤:Python python-3.x 硒 子进程
上一篇:將6位儒略日期轉換為標準日期
