Why is my subprocess script not running my selenium python script? - python

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

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

Display print messages in console PyInstaller

I'm using PyInstaller to convert my Python file to .exe; however, I see that upon running the executable file, I don't see any print messages in the terminal which I use for my reference - it's just blank (no print messages).
Command I used: pyinstaller --onefile -w myfile.py
I use Selenium module in my Python file, which uses chromedriver.exe, so after running the generated .exe file, this is what it shows:
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from selenium.common.exceptions import TimeoutException
from time import sleep
import random
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
print("Opened the browser")
Pictures:
I need to see the print messages. Tried searching for answers but couldn't find any solutions!
How do I resolve this?
If you run your .exe from a terminal like PowerShell it will probably show the output you're looking for. The window in the screenshot looks like it is coming from the Selenium driver.
If you want this to work when double-clicking on the .exe, rebuild without the -w flag:
-w, --windowed, --noconsole
Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS this also triggers building a Mac OS .app bundle. On Windows this option is automatically set if the first script is a ‘.pyw’ file. This option is ignored on *NIX systems.

Scraper automatically exits

I am creating a LinkedIn scraper using Selenium. My script executes properly but it gets close after 3-5 secs and it does not give any error in the console. It says Process finished with exit code 0
Here is my code:
import time,os
try:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
except:
os.system("python -m pip install --upgrade pip")
os.system("pip3 install selenium")
os.system("pip3 install webdriver-manager")
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
driver.get("https://www.linkedin.com/")

Selenium launch browser in specific path (for example ./firefox/firefox.exe) (Python3)

I would like to do what is in the title.
My code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.google.com')
Have you tried webdriver-manager library?
It's amazing, you just need to install it:
pip install webdriver-manager
and launch your code like this:
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
that package downloads you needed geckodriver automatically. Don't forget path 'executable_path' while launching driver.
As well you can you chromedriver, like that:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
Selenium searches the webdriver through the directories on sys.path, so you would first have to do something like that:
import sys
from selenium import webdriver
sys.path.insert(0,'/path/to/firefox')
driver = webdriver.Firefox()
driver.get('https://www.google.com')

I have already installed webdriver_manager but python can't find the module

This is from my script:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome('C:\\Users\\tques\\chromedriver_win32\\chromedriver.exe')
driver = webdriver.Chrome(ChromeDriverManager().install())
I have tried both instances of the "driver" variable, both independently and together but I always get this error "ModuleNotFoundError: No module named 'webdriver_manager'"
I've already tried uninstalling selenium and webdriver_manager, then reinstalling them, but the issue persists.
find the file path ware webdriver_manager is installed, then add
import sys
sys.path.append(<webdriver_manager path>)
before
from webdriver_manager.chrome import ChromeDriverManager
Have you tried to add a driver to the PATH? - HOW TO DO THIS
And then do as simple as:
from selenium import webdriver
browser = webdriver.Chrome()
try this:
from selenium import webdriver<br>
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

Python Launching Chromedriver from selenium works in IDLE but not Visual Studio Code

Below is my code and when I run it through powershell/cmd/IDLE it launches the chromedriver perfectly. However when I try to run it through visual studio code I receive the text below.
"selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home"
#SELENIUM INFO#
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
#SELENIUM INFO#
import os
print(os.getcwd())
driver = webdriver.Chrome(os.getcwd() +'\\chromedriver.exe')
I right click the code and click 'Run Python File in Terminal' However I've discovered that I can't even use the terminal to cd to the code deeper inside the file structure.
The file structure I have is "G:\Quality User Data\Malahy\Projects\AQE Interfaces>"
But the chrome driver and the python file are stored together in a deeper directory.
"G:\Quality User Data\Malahy\Projects\AQE Interfaces\AQE Interface\Logistics\Repack"
I'm starting to think this is a bug with Visual Studio Code more than my program.

Categories