I have an app that needs to open Chrome and automatically send WhatsApp messages. When I run python main.py, everything works as expected. However, when I try to generate a .exe using PyInstaller so that non-Python users can run it, I get the following error: session not created: failed to start Chrome process.
Here is the relevant information:
The chrome.exe and chromedriver versions I am using: both 135.0.7049.84
The code that performs this task:
import pickle
import time
import urllib.parse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
def run():
with open("messages.pkl", "rb") as file:
messages = pickle.load(file)
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument(r"--user-data-dir=C:\Users\Urano\AppData\Local\Google\Chrome\User Data")
options.add_argument("--profile-directory=Default")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
wait = WebDriverWait(driver, 30)
print("Abriendo WhatsApp Web...")
driver.get("https://web.whatsapp.com")
input("Escaneá el código QR y presioná ENTER cuando WhatsApp Web esté cargado...\n")
for phone, message in messages:
try:
print(f"\n📨 Preparando mensaje para {phone}")
encoded_message = urllib.parse.quote(message)
url = f"https://web.whatsapp.com/send?phone={phone}&text={encoded_message}"
driver.get(url)
print("🔄 Esperando que cargue el campo de texto...")
message_box = wait.until(
EC.presence_of_element_located((By.XPATH, '//div[@data-tab="10"]'))
)
print("✅ Campo de texto localizado")
time.sleep(2)
message_box.send_keys(Keys.ENTER)
print(f"✅ Mensaje enviado a {phone}")
time.sleep(3)
except Exception as e:
print(f"❌ Error al enviar mensaje a {phone}: {e}")
continue
driver.quit()
print("\n✅ Todos los mensajes procesados.")
if __name__ == "__main__":
run()
And my project structure (before running any pyinstaller command):
How can I run the pyinstaller to fix this issue?
The problem is that Chrome is not in the same directory or path with all of the users
so I tried to make this code, and it is a small part of yours because you did not provide some additional files like "messages.pkl"
I have deleted this line :
options.add_argument(r"--user-data-dir=C:\Users\Urano\AppData\Local\Google\Chrome\User Data")
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
def run():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--profile-directory=Default")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
print("Abriendo WhatsApp Web...")
driver.get("https://web.whatsapp.com")
input("Escaneá el código QR y presioná ENTER cuando WhatsApp Web esté cargado...\n")
driver.quit()
print("\n✅ Todos los mensajes procesados.")
if __name__ == "__main__":
run()
then converted it into exe file using this command:
pyinstaller --noconfirm --onefile --console "your_file.py" --workpath "Final_exe_file/build" --distpath "Final_exe_file/exe"