Home > Software engineering >  Why is my subprocess script not running my selenium python script?
Why is my subprocess script not running my selenium python script?

Time:04-26

I'm trying to run multiple Python scripts using subprocess. 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. 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. 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. Any ideas as to what could be going wrong?.

If you would like to test it out for yourself. Here is 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()

CodePudding user response:

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. Try Popen maybe:

subprocess.Popen(["python3", "C:/Users/David/Desktop/script1.py"], shell=True)

Edit2: The problem was solved by changing python3 to python.

  • Related