How do I exit a Price Check Loop once desired or lower price is met and commit to purchase?

Viewed 20

I'm trying to to automate an amazon purchase when the right set price hits in the code compared to to current amazon price.

It seems that it just goes into infinite loop rather than refresh the page and execute once the set price or lower hits. Is there an easier way to code it?

from selenium import webdriver as wd
import chromedriver_binary
import random
import time

wd = wd.Chrome()
wd.implicitly_wait(10)

wd.get("https://www.amazon.ca/dp/B0951JZDWT/ref=olp-opf-redir?aod=1&ie=UTF8&condition=ALL")

random_wait_time = random.randrange(2.0, 7.0)
print(random_wait_time)
time.sleep(random_wait_time)

price_id = "aod-price-1"
# find top price on page

current_price = wd.find_element_by_id(price_id).text

print(current_price)

disallowed_characters = "$\n"

for character in disallowed_characters:
    current_price = current_price.replace(character, "")

print(current_price)

amazon_price = int(current_price)

buy_price = int(5500)
# limit price to purchase item

while (amazon_price) >= int(buy_price):
    print(amazon_price)
    print('do not buy')
    random_wait_time = random.randrange(8.0, 20.0)
    print(random_wait_time)
    time.sleep(random_wait_time)
    wd.refresh()
else:
    add_to_cart_button = wd.find_element_by_xpath('//*[@id="a-autoid-2-offer-1"]/span/input')
    add_to_cart_button.click()

view_cart_button = wd.find_element_by_xpath('//*[@id="aod-offer-view-cart-1"]/span/input')

view_cart_button.click()
0 Answers
Related