Reputation: 135
Sorry if this question is worded weirdly I just started using Selenium. I want to make a program that lets someone know when certain flavors of certain vapes are almost out of stock on a wholesale website. So far, my program is really simple and will go the url
of a specific vape and print out the text from the table showing different flavors of vapes and their stock.
However, I want to check the text under stock quantity (the number of vapes left) and print "(this this specific vape flavor) is almost out of stock!" if the number is below 10. Here is my code so far:
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
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://safagoods.com/vape-shop/disposable-vape-devices/huge-disposables")
element = driver.find_element_by_xpath("//*[@id='input-option2231']").text
print(element)
driver.close()
Any tips? I don't plan on doing this with every single vape on the website, just the very popular ones so the person using it can run the program and get an idea of what they should order first and what they can wait to order. Thanks guys.
Upvotes: 1
Views: 721
Reputation: 4212
As a possible solution, you can check if the value is less then x and append it to your list afterwards. My solution will print you a list where the amount is less than 20.
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/huge-disposables")
wait = WebDriverWait(driver, 15)
table = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".table.table-bordered tr[style='background: #eeeeee;']")))
data = []
rows = driver.find_elements_by_css_selector(".table.table-bordered tr[style='background: #eeeeee;']")
for row in rows:
#qty = row.find_element_by_xpath("./td[1]").text
stock = row.find_element_by_xpath("./td[2]").text
name = row.find_element_by_xpath("./td[3]").text
if int(stock) < 20:
data.append([stock, name])
print(*data, sep='\n')
Output:
['17', 'Banana Ice (SKU: HUGE-BI )']
['17', 'Lush Ice (SKU: HUGE-LI )']
You can also print it without brackets with:
for x in data:
print(' '.join(x))
This will give you the output:
17 Banana Ice (SKU: HUGE-BI )
17 Lush Ice (SKU: HUGE-LI )
Upvotes: 2