Search code examples
pythoncssseleniumselenium-webdriverweb-scraping

How to click and print multiple elements in Selenium/Python?


My dad runs a business and has a specific wholesale website he goes to, but I've noticed when he's buying disposable vapes, he has to click on each vape and then look at the stock of each flavor of that vape. I'd like to create a program which prints out the stock of every flavor for every disposable vape. So far, I go to the URL for each vape and then inspect the table which shows the stock of the different flavors and print that table out. But, I don't want to enter the specific URL for every single vape then type print for every single stock table. Is there a cleaner way to just use the URL for the page that lists all the disposable vapes using ""driver.get('https://safagoods.com/vape-shop/disposable-vape-devices/')"" and then from there click on all the different vapes and print out their stock table? Sorry if this is a confusion question. Hopefully if my code is ran it explains it a bit better:

from selenium import webdriver

import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.set_window_size(2000,1000)

driver.get('https://safagoods.com/vape-shop/disposable-vape-devices/huge-disposables')
HUGE_vape = driver.find_element_by_id('input-option2231')
print(HUGE_vape.text)
driver.get('https://safagoods.com/vape-shop/disposable-vape-devices/kangvape-onee-max-disposables')
KANG_vape = driver.find_element_by_id('input-option2228')
print(KANG_vape.text)
driver.get('https://safagoods.com/vape-shop/disposable-vape-devices/art-x-disposables')
ART_vape = driver.find_element_by_id('input-option2196')
print(ART_vape.text)


driver.close()

I have three vapes so far but there are 57 different ones and sometimes they update the merchandise. Instead of typing the information for every single vape, I'd like to know if there's a way to start on the initial disposable vape page I posted above and print out the stock of all available vapes. If there isn't I'm OK with typing them all out, just curious to see if there's a better option.


Solution

  • It should be easy if you scrape the main page with your products:

    SOLUTION

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
    
    driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/")
    wait = WebDriverWait(driver, 15)
    vapes = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".caption>.name>a")))
    vape_names = []
    for vape in vapes:
        vape_names.append(vape.text)
    print(*vape_names, sep='\n')
    

    Result

    HUGE (BRAND) DISPOSABLE VAPE DEVICES 10PC [3000 PUFFS]
    KANGVAPE ONEE MAX DISPOSABLE VAPE DEVICES 10PC [5000 PUFFS]
    ART X DISPOSABLES 10PC [5000 PUFFS]
    FUME INFINITY DISPOSABLE VAPE DEVICES 5PC DISPLAY BOX
    SEA AIR PLUS DISPOSABLE VAPES 10PC
    HYDE RETRO RECHARGE DISPOSABLES 10PC DISPLAY BOX
    

    and so on...

    Technically, you create an empty list and add text values there. Note how I use css selectors and explicit waits.