Display print messages in console PyInstaller - python

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.

Related

Deadlock with the launch of chromedriver on Ubuntu 20.04 LTS (python)

I've trying to use seelnium chromedriver on Ubuntu 20.04 LTS (everything going well on work machine) for telegram bot.
Here is my imports and configs:
import selenium.webdriver.chrome.options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from xvfbwrapper import Xvfb
from webdriver_manager.chrome import ChromeDriverManager
import config
path = config.PATH #reading from different file (config) where PATH=r'/usr/bin/chromedriver'
Options = selenium.webdriver.chrome.options.Options()
Options.add_argument("--no--sandbox")
Options.add_argument('--headless')
Options.add_argument('log-level=3')
Options.add_argument("--remote-debugging-port=9222")
Here is info from server console:
whereis chromedriver > chromedriver: /usr/bin/chromedriver
chromedriver --version > ChromeDriver 104.0.5112.79 (3cf3e8c8a07d104b9e1260c910efb8f383285dc5-refs/branch-heads/5112#{#1307})
google-chrome --version > Google Chrome 104.0.5112.101
Here is part of code regarding selenium work:
#dp.message_handler(commands=['useragent'])
async def command_useragent(message: types.Message):
if message.from_user.id in admin:
Options.add_argument(f'user-agent={random.choice(config.user_agents_list)}')
vdisplay = Xvfb(width=1280, height=740, colordepth=16)
vdisplay.start()
time.sleep(10)
browser = webdriver.Chrome(service=Service(rf'{config.PATH}'), options=Options)
browser.get('https://google.com')
vdisplay.stop()
else:
pass
somewhy following string of code doesnt work:
browser = webdriver.Chrome(service=Service(rf'{config.PATH}'),options=Options)
I've tried to delete, change executable path, use or not use options "--no--sandbox", '--headless', "--remote-debugging-port=9222", use or not use pyvirtualdisplay and xvfbwrapper. I've absolutely confused how it should work.
here is part of error message regarding this code:
selenium.common.exceptions.WebDriverException: Message: unknown error:
Chrome failed to start: exited abnormally. (chrome not reachable)
(The process started from chrome location
/snap/chromium/2076/usr/lib/chromium-browser/chrome is no longer
running, so ChromeDriver is assuming that Chrome has crashed.)
Would anyone know how to fix this? Sorry if this is stupid question - I'm new in it. Thank you for advices.

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

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.

How to auto download .exe files to project directory based on browser version using Webdriver Manager in Python Robotframework

I tried below code but its downloaded and save it the path we configured in pip list [2nd line of code]. Instead of auto download in my local machine, I want to download directly into project directory because of few restriction issues in my organisation. Can anyone provide suggestions on this?
pip install webdrivermanager
webdrivermanager firefox chrome --linkpath /usr/local/bin
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
def get_chromedriver_path():
driver_path = ChromeDriverManager().install()
print(driver_path)
return driver_path
Library chromedriversync.py
${chromedriver_path}= chromedriversync.Get Chromedriver Path
Create Webdriver chrome executable_path=${chromedriver_path}
Go to www.google.com
Finally I got the solution. Below query helped me to auto download .exe files of the respective browser version to Project Current directory.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from lib2to3.tests.support import driver
import os
import os.path
from os.path import curdir
def get_chromedriver_path():
ReturnPath = os.getcwd()
driver_path = ChromeDriverManager(path=ReturnPath).install()
print(driver_path)
return driver_path
Library browser
${driver}= browser.Get Chromedriver Path
log ${driver}
Create Webdriver Chrome executable_path=${driver} chrome_options=${options}

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.

chromedriver not found error on heroku app

I want to use selenium on my heroku app. So I added following buildpacks.
Following is the python code which uses the selenium, chrome and chromedriver
from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
import os
import pickle
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_bin = os.environ.get('GOOGLE_CHROME_SHIM', None)
opts = ChromeOptions()
opts.binary_location = chrome_bin
driver = webdriver.Chrome(executable_path="chromedriver",
chrome_options=opts)
I am getting following error
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'
Please help me.
I am answering for anyone who has same problem. So the problem was when I was adding buildpacks then it was asking me to run "git push heroku master" to use buildpack in app but that was not working. You need to make change in code and the you should push code on git so that it app runs again it rebuild itself.

Categories