我有两个selenium python代码.我用一个子流程脚本运行它们:

import subprocess
subprocess.run("python script1.py & python script2.py", cwd=r'C:\Users\David\Desktop\Selenium', shell=True)

When I run them, they don't run in parallel but rather they run sequentially.有人知道我如何调整这个脚本,使它们同时(并行)运行吗?

以下是脚本1的代码&2:

script1.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()

script2.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()

推荐答案

我不确定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()

Python相关问答推荐

Python panda拆分列保持连续多行

如果索引不存在,pandas系列将通过索引获取值,并填充值

jit JAX函数中的迭代器

如何调整spaCy token 化器,以便在德国模型中将数字拆分为行末端的点

试图找到Python方法来部分填充numpy数组

图像 pyramid .难以创建所需的合成图像

如何让程序打印新段落上的每一行?

NumPy中条件嵌套for循环的向量化

创建可序列化数据模型的最佳方法

为一个组的每个子组绘制,

多指标不同顺序串联大Pandas 模型

* 动态地 * 修饰Python中的递归函数

当单元测试失败时,是否有一个惯例会抛出许多类似的错误消息?

在numpy数组中寻找楼梯状 struct

无法在Spyder上的Pandas中将本地CSV转换为数据帧

根据Pandas中带条件的两个列的值创建新列

Tensorflow tokenizer问题.num_words到底做了什么?

使用Python TCP套接字发送整数并使用C#接收—接收正确数据时出错

在我融化极点数据帧之后,我如何在不添加索引的情况下将其旋转回其原始形式?

为什么在Python中00是一个有效的整数?