this post was submitted on 26 Mar 2024
4 points (100.0% liked)

Selenium Web Browser Automation

100 readers
4 users here now

Users forum for selenium browser testing. Do not advertise here.

founded 1 year ago
MODERATORS
 

Hi! I have developed an application for parsing store products with uploading product cards to telegram. And it works, but only on a local computer. When the application is uploaded to the hosting, the program doesn't show any errors, but the product cards don't appear. Please help me.
main.py

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from congif import *
def ex(func):
   async def show_ex(*args, **kwargs):
       try:
           await func(*args, **kwargs)
       except Exception as e:
           print('Error: ', e)
   return show_ex
@ex
async def create_table(cursor, word):
   try:
       await cursor.execute(f'''
           CREATE TABLE IF NOT EXISTS {word.lower()} (
               id INTEGER PRIMARY KEY AUTOINCREMENT,
               price TEXT,
               link TEXT UNIQUE,
               img TEXT
           )
       ''')
   except sqlite3.OperationalError:
       time.sleep(10)
       await cursor.execute(f'''
                   CREATE TABLE IF NOT EXISTS {word.lower()} (
                       id INTEGER PRIMARY KEY AUTOINCREMENT,
                       price TEXT,
                       link TEXT UNIQUE,
                       img TEXT
                   )
               ''')
@ex
async def insert_data(cursor, word, new_price, link, img):
   try:
       await cursor.execute(f'''
           INSERT INTO {word.replace(" ", "").lower()} (price, link, img) VALUES (?, ?, ?)
       ''', (new_price, link, img))
   except sqlite3.IntegrityError:
       pass
   except sqlite3.OperationalError:
       time.sleep(10)
       await cursor.execute(f'''
                   INSERT INTO {word.replace(" ", "").lower()} (price, link, img) VALUES (?, ?, ?)
               ''', (new_price, link, img))
@ex
async def check_table_exists(conn, table_name):
   async with conn.cursor() as cursor:
       await cursor.execute(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table_name.lower()}';")
       return await cursor.fetchone() is not None
@ex
async def send_product_messages(user_id, card, img_url):
   try:
       await bot.send_photo(user_id, photo=img_url, caption=card)
   except Exception as e:
       print(f"Error: {e}")
@ex
async def process_user(user_id, products):
   conn = await aiosqlite.connect('/data/mercaridata.db')
   async with conn.cursor() as cursor:
       tasks = []
       for product_key, product_value in products.items():
           table_exists = await check_table_exists(conn, product_value)
           if not table_exists:
               await create_table(cursor, product_value.replace(" ", ''))
           url = f'https://jp.mercari.com/search?keyword={product_value.replace(" ", "%20")}&sort=created_time&order=desc'
           task = asyncio.create_task(process_product(user_id, cursor, product_value, url))
           tasks.append(task)
       await asyncio.gather(*tasks)
       await conn.commit()
@ex
async def process_product(user_id, cursor, product_value, url):
   options = webdriver.ChromeOptions()
   options.add_argument("--headless")
   options.add_argument("--no-sandbox")
   options.add_argument("--disable-gpu")
   options.add_argument("--remote-debugging-port=9230")
   driver = webdriver.Chrome(options=options)
   try:
       driver.maximize_window()
       driver.get(url)
       card_divs = driver.find_elements(By.XPATH, '//li[@class="sc-bcd1c877-2 gWHcGv"]')[:10]
       for card_div in card_divs:
           try:
               price = float(
                   (card_div.find_element(By.TAG_NAME, 'span').text.replace(' ', '').replace(',', '').
                    replace('\n', '')))
           except ValueError:
               price = 'SALE!'
           except Exception:
               price = '?'
           try:
               link = card_div.find_element(By.TAG_NAME, 'a').get_attribute('href')
           except Exception:
               continue
           img = card_div.find_element(By.TAG_NAME, 'img').get_attribute('src')
           await insert_data(cursor, product_value, price, link, img)
           print(price, link, img)
           card = (f"Item: {link} \n"
                   f"Price: {price} $.\n")
           if card:
               await send_product_messages(user_id, card, img)
           else:
               print('ТNo items')
               await bot.send_message(user_id, 'No items')
   finally:
       driver.stop_client()
       driver.close()
       driver.quit()
 
no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here