pythonselenium-webdriverherokuselenium-chromedriver

chromedriver and firefoxdriver crashing/stopping on heroku


i am trying to host my own bot that will price check tablet that i want to buy and send the message if price change on discord, i am using heroku cause i have free credits from GitHub student developement pack, but when i run it i get

selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 255

i read to owngrade firefox but how should i do it on heroku? using chromedriver gives

2024-10-31T15:09:07.674086+00:00 app[worker.1]: selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally. 2024-10-31T15:09:07.674086+00:00 app[worker.1]: (session not created: DevToolsActivePort file doesn't exist) 2024-10-31T15:09:07.674098+00:00 app[worker.1]: (The process started from chrome location /app/.cache/selenium/chrome/linux64/130.0.6723.91/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

both works on my pc i tried installing heroku-integrated-firefox-geckodriver buildpack but it didnt work as i need heroku version 20, 18 or 16 and i have 24 and i dont know how to downgrade

src with ChromeDriver:

import asyncio
import discord
from discord.ext import commands
import time
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from fake_useragent import UserAgent


intents = discord.Intents.default()
intents.members = True
intents.message_content = True

bot = commands.Bot(command_prefix='?', description="Checking price lol", intents=intents)

# Initialize global variables
global xkom_price_nokb, xkom_price, xkom_price_lte, lastchecked
xkom_price = None
xkom_price_lte = None
lastchecked = None
xkom_price_nokb = None

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user} (ID: {bot.user.id})')
    print('------')
    channel = bot.get_channel(1299401756723249246)
    
    # Initial Embed Message
    embed = discord.Embed(title="Lenovo Tab P11 6GB/128GB", color=0x39153e)
    embed.add_field(name="x-kom", value="", inline=False)
    embed.add_field(name="WiFi (no keyboard)", value="Loading...", inline=True)
    embed.add_field(name="WiFi", value="Loading...", inline=True)
    embed.add_field(name="WiFi/LTE", value="Loading...", inline=True)
    embed.add_field(name="Last checked", value="Loading...", inline=False)
    
    if channel:
        async for message in channel.history(limit=None):
            await message.delete()
    else:
        await channel.send(f'Error: Channel not found.')

    global embedsent, embedskirtsent
    embedsent = await channel.send(embed=embed)
    
    # Start monitoring prices
    await monitor_prices()

async def fetch_price(url, whatclass):
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument("--incognito")
    options.add_argument("--nogpu")
    options.add_argument("--disable-gpu")
    options.add_argument("--window-size=1280,1280")
    options.add_argument("--no-sandbox")
    options.add_argument("--enable-javascript")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument('--disable-blink-features=AutomationControlled')

    ua = UserAgent()
    userAgent = ua.random

    driver = webdriver.Chrome(options=options)
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": userAgent})
    
    driver.get(url)

    # output the full-page HTML
    source = driver.page_source

    # release the resources allocated by Selenium and shut down the browser
    driver.quit()

    # Parse the HTML
    soup = BeautifulSoup(source, 'html.parser')
    price_span = soup.find('span', class_=whatclass)
    price = price_span.get_text(strip=True) if price_span else None
    print(price)
    return price


async def monitor_prices():
    global xkom_price, xkom_price_nokb, xkom_price_lte, lastchecked  # Declare them as global
    while True:
        # Fetch prices for the two URLs
        xkom_new_price_nokb = await fetch_price("https://www.x-kom.pl/p/1126296-tablety-11-lenovo-tab-p11-6gb-128gb-android-12l-wifi-gen-2.html", 'sc-jnqLxu cjLwnY parts__Price-sc-53da58c9-2 hbVORa')
        await asyncio.sleep(3)
        xkom_new_price_lte = await fetch_price("https://www.x-kom.pl/p/1145247-tablety-11-lenovo-tab-p11-6gb-128gb-android-12l-lte-gen-2.html", 'sc-jnqLxu cjLwnY parts__Price-sc-53da58c9-2 hbVORa')
        await asyncio.sleep(3)
        xkom_new_price = await fetch_price("https://www.x-kom.pl/p/1152486-tablety-11-lenovo-tab-p11-6gb-128gb-android-12l-wifi-gen-2.html", 'sc-jnqLxu cjLwnY parts__Price-sc-53da58c9-2 hbVORa')
        await asyncio.sleep(3)

        # Get the current time as the last checked time
        lastchecked = time.strftime('%Y-%m-%d %H:%M:%S')

        # Check if prices have changed
        channel2 = bot.get_channel(1299401805683494942)

        if xkom_new_price_nokb and xkom_new_price_nokb != xkom_price_nokb:
            await channel2.send(f"WiFi (no keyboard) price on x-kom changed from {xkom_price_nokb} to {xkom_new_price_nokb}")
            xkom_price_nokb = xkom_new_price_nokb

        if xkom_new_price and xkom_new_price != xkom_price:
            await channel2.send(f"WiFi price on x-kom changed from {xkom_price} to {xkom_new_price}")
            xkom_price = xkom_new_price
        if xkom_new_price_lte and xkom_new_price_lte != xkom_price_lte:
            await channel2.send(f"WiFi/LTE price on x-kom changed from {xkom_price_lte} to {xkom_new_price_lte}")
            xkom_price_lte = xkom_new_price_lte

        # Update the embed message
        embed = discord.Embed(title="Lenovo Tab P11 6GB/128GB", color=0x39153e)
        embed.add_field(name="x-kom", value="", inline=False)
        embed.add_field(name="WiFi (no keyboard)", value=xkom_new_price_nokb or "Not available", inline=True)
        embed.add_field(name="WiFi", value=xkom_price or "Not available", inline=True)
        embed.add_field(name="WiFi/LTE", value=xkom_price_lte or "Not available", inline=True)
        embed.add_field(name="Last checked", value=lastchecked, inline=False)
        await embedsent.edit(embed=embed)

        # Wait for 5 minutes
        await asyncio.sleep(300)


# Run the bot
bot.run('token')

src with FirefoxDriver:

import asyncio
import discord
from discord.ext import commands
import time
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

intents = discord.Intents.default()
async def fetch_price(url, whatclass):
    options = Options()
    options.add_argument("-headless")
    options.set_preference("javascript.enabled", True)

    driver = webdriver.Firefox(options=options)
    driver.get(url)

    # output the full-page HTML
    source = driver.page_source

    # release the resources allocated by Selenium and shut down the browser
    driver.quit()

    # Parse the HTML
    soup = BeautifulSoup(source, 'html.parser')
    price_span = soup.find('span', class_=whatclass)
    price = price_span.get_text(strip=True) if price_span else None
    print(price)
    return price


async def monitor_prices():
    global xkom_price, xkom_price_nokb, xkom_price_lte, lastchecked  # Declare them as global
    while True:
        # Fetch prices for the two URLs
        xkom_new_price_nokb = await fetch_price("https://www.x-kom.pl/p/1126296-tablety-11-lenovo-tab-p11-6gb-128gb-android-12l-wifi-gen-2.html", 'sc-jnqLxu cjLwnY parts__Price-sc-53da58c9-2 hbVORa')
        await asyncio.sleep(3)
        xkom_new_price_lte = await fetch_price("https://www.x-kom.pl/p/1145247-tablety-11-lenovo-tab-p11-6gb-128gb-android-12l-lte-gen-2.html", 'sc-jnqLxu cjLwnY parts__Price-sc-53da58c9-2 hbVORa')
        await asyncio.sleep(3)
        xkom_new_price = await fetch_price("https://www.x-kom.pl/p/1152486-tablety-11-lenovo-tab-p11-6gb-128gb-android-12l-wifi-gen-2.html", 'sc-jnqLxu cjLwnY parts__Price-sc-53da58c9-2 hbVORa')
        await asyncio.sleep(3)

        # Get the current time as the last checked time
        lastchecked = time.strftime('%Y-%m-%d %H:%M:%S')

        # Check if prices have changed
        channel2 = bot.get_channel(1299401805683494942)

        if xkom_new_price_nokb and xkom_new_price_nokb != xkom_price_nokb:
            await channel2.send(f"WiFi (no keyboard) price on x-kom changed from {xkom_price_nokb} to {xkom_new_price_nokb}")
            xkom_price_nokb = xkom_new_price_nokb

        if xkom_new_price and xkom_new_price != xkom_price:
            await channel2.send(f"WiFi price on x-kom changed from {xkom_price} to {xkom_new_price}")
            xkom_price = xkom_new_price
        if xkom_new_price_lte and xkom_new_price_lte != xkom_price_lte:
            await channel2.send(f"WiFi/LTE price on x-kom changed from {xkom_price_lte} to {xkom_new_price_lte}")
            xkom_price_lte = xkom_new_price_lte

        # Update the embed message
        embed = discord.Embed(title="Lenovo Tab P11 6GB/128GB", color=0x39153e)
        embed.add_field(name="x-kom", value="", inline=False)
        embed.add_field(name="WiFi (no keyboard)", value=xkom_new_price_nokb or "Not available", inline=True)
        embed.add_field(name="WiFi", value=xkom_price or "Not available", inline=True)
        embed.add_field(name="WiFi/LTE", value=xkom_price_lte or "Not available", inline=True)
        embed.add_field(name="Last checked", value=lastchecked, inline=False)
        await embedsent.edit(embed=embed)

        # Wait for 5 minutes
        await asyncio.sleep(300)


# Run the bot
bot.run('token')

changing webdriver from firefox to chrome, error changed but still doesn't work, i tried downloading firefox binary manually and deploying it with a repo but it didnt work, i tried installing heroku-integrated-firefox-geckodriver buildpack but it didnt work as i need heroku version 20, 18 or 16 and i have 24 and i dont know how to downgrade


Solution

  • update: it worked after instaling this buildpack