[英]Why is my subprocess script not running my selenium python script?
I'm trying to run multiple Python scripts using subprocess.我正在尝试使用子进程运行多个 Python 脚本。 As a trial I just wanted to see if one script would run.
作为试验,我只是想看看是否有一个脚本可以运行。 The script is currently on the Desktop.
该脚本当前位于桌面上。 I am using Spyder to run my script.
我正在使用 Spyder 来运行我的脚本。 I run my script with the following code:
我使用以下代码运行我的脚本:
import subprocess
subprocess.run(
"python3 script1.py",
cwd=r'C:\Users\David\Desktop',
shell=True,
)
I don't receive any errors when I run this but the script1.py itself doesn't run.运行此程序时我没有收到任何错误,但 script1.py 本身并未运行。 I know this because the script1.py is a selenium code which is supposed to download a file from a website but it doesn't download the file when I use the code above.
我知道这是因为 script1.py 是一个 selenium 代码,它应该从网站下载文件,但当我使用上面的代码时它不会下载文件。 Any ideas as to what could be going wrong?.
关于可能出什么问题的任何想法?
If you would like to test it out for yourself.如果您想亲自测试一下。 Here is script1.py :
这是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()
options.add_argument("--headless")
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()
Try:尝试:
import subprocess
subprocess.run(["python3", "C:/Users/David/Desktop/script1.py"], shell=True)
Edit: I have used Popen
like this and it seemed to work fine.编辑:我像这样使用
Popen
,它似乎工作正常。 Try Popen
maybe:也许试试
Popen
:
subprocess.Popen(["python3", "C:/Users/David/Desktop/script1.py"], shell=True)
Edit2: The problem was solved by changing python3
to python
. Edit2:通过将
python3
更改为python
解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.