Tuesday, 2 February 2021

Selenium Chrome Webdriver process Working Locally But Not On Heroku

So I have the following Selenium Chrome Webdriver process that is working correctly locally. Basically, when the below code is run, a purchase is made through Best Buy, and an SMS message is sent to me from my Twilio phone number indicating whether or not the purchase was successful. The variables in all-caps are already defined. I also did not include the imports. Again, the below code is making the purchase SUCCESSFULLY when run locally. This means that the SMS message that I get from my Twilio phone number contains "Success!" every time the following code is run locally:

client = Client(ACCOUNT_SID, AUTH_TOKEN)
def runBestBuyBot():
    driver = webdriver.Chrome()
    driver.get("https://www.bestbuy.com/site/spongebob-squarepants-mini-plush-styles-may-vary/6404213.p?skuId=6404213")
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body[@class='size-l']/div[@class='pl-page-content']//div[@class='container-v2']/div[@class='row v-m-bottom-g']/div[2]//div[@class='col-xs-12']/div[6]/div[@class='None']/div/div/div/button[@type='button']")))
    element.send_keys(Keys.RETURN)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@class='c-portal']/div[@role='dialog']/div[1]/div[@role='dialog']/div[@role='document']//a[@role='button']")))
    element.send_keys(Keys.RETURN)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@id='cartApp']/div[@class='page-spinner page-spinner--out']/div[@class='large-view size-l']//div[@class='fluid-large-view']//section[@class='fluid-large-view__sidebar']//button[@type='button']")))
    element.send_keys(Keys.RETURN)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='fld-e']")))
    element.send_keys(EMAIL)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='fld-p1']")))
    element.send_keys(PASSWORD)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@class='cia-app-container']/div[@class='cia-actual-full-page-wrapper lv']/section/main[@class='cia-wrapper container']//form/div[3]/button")))
    element.send_keys(Keys.RETURN)
    driver.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='credit-card-cvv']"))))
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='credit-card-cvv']")))
    element.send_keys(CVV)
    driver.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html//div[@id='checkoutApp']/div[@class='page-spinner page-spinner--out']/div[1]/div[1]//div[@class='checkout__container checkout__container-fast-track']/div[@class='checkout__col checkout__col--primary']//div[@class='button--place-order-fast-track']/button"))))
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@id='checkoutApp']/div[@class='page-spinner page-spinner--out']/div[1]/div[1]//div[@class='checkout__container checkout__container-fast-track']/div[@class='checkout__col checkout__col--primary']//div[@class='button--place-order-fast-track']/button")))
    element.send_keys(Keys.RETURN)
    
try:
    runBestBuyBot()
    message = client.messages \
        .create(
            body='Success!',
            from_=TWILIONUMBER,
            to=MYNUMBER
        )
except Exception as e:
    message = client.messages \
        .create(
            body='Fail! ' + str(e),
            from_=TWILIONUMBER,
            to=MYNUMBER
        )

The following code is the basically the same process but adapted slightly to function on Heroku remote dyno. It is a part of my Heroku web app. The below code is NOT making the purchase successfully when run on Heroku remote dyno. This means that the SMS message that I get from my Twilio phone number contains "Fail!" every time the following code is run on the Heroku remote dyno:

def runBestBuyBot():
    chrome_options = webdriver.ChromeOptions()
    chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
    driver.get("https://www.bestbuy.com/site/spongebob-squarepants-mini-plush-styles-may-vary/6404213.p?skuId=6404213")
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body[@class='size-l']/div[@class='pl-page-content']//div[@class='container-v2']/div[@class='row v-m-bottom-g']/div[2]//div[@class='col-xs-12']/div[6]/div[@class='None']/div/div/div/button[@type='button']")))
    element.send_keys(Keys.RETURN)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@class='c-portal']/div[@role='dialog']/div[1]/div[@role='dialog']/div[@role='document']//a[@role='button']")))
    element.send_keys(Keys.RETURN)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@id='cartApp']/div[@class='page-spinner page-spinner--out']/div[@class='large-view size-l']//div[@class='fluid-large-view']//section[@class='fluid-large-view__sidebar']//button[@type='button']")))
    element.send_keys(Keys.RETURN)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='fld-e']")))
    element.send_keys(EMAIL)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='fld-p1']")))
    element.send_keys(PASSWORD)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@class='cia-app-container']/div[@class='cia-actual-full-page-wrapper lv']/section/main[@class='cia-wrapper container']//form/div[3]/button")))
    element.send_keys(Keys.RETURN)
    driver.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='credit-card-cvv']"))))
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//input[@id='credit-card-cvv']")))
    element.send_keys(CVV)
    driver.execute_script("arguments[0].scrollIntoView(true);", WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html//div[@id='checkoutApp']/div[@class='page-spinner page-spinner--out']/div[1]/div[1]//div[@class='checkout__container checkout__container-fast-track']/div[@class='checkout__col checkout__col--primary']//div[@class='button--place-order-fast-track']/button"))))
    element = wait.until(EC.presence_of_element_located((By.XPATH, "/html//div[@id='checkoutApp']/div[@class='page-spinner page-spinner--out']/div[1]/div[1]//div[@class='checkout__container checkout__container-fast-track']/div[@class='checkout__col checkout__col--primary']//div[@class='button--place-order-fast-track']/button")))
    element.send_keys(Keys.RETURN)

client = Client(ACCOUNT_SID, AUTH_TOKEN)
try:
    runBestBuyBot()
    message = client.messages \
        .create(
            body='Success!',
            from_=TWILIONUMBER,
            to=MYNUMBER
        )
except Exception as e:
    message = client.messages \
        .create(
            body='Fail!' + str(e),
            from_=TWILIONUMBER,
            to=MYNUMBER
        )

I'm trying to get the message of the Exception by using str(e) to see what exactly is going wrong when the above code is run on the Heroku remote dyno, but str(e) is always coming out as "Message:", so it's not giving me any indication as to what line within runBestBuyBot is causing the exception to be thrown.

So, the question is: does anyone have an idea as to why runBestBuyBot is NOT throwing an exception when run locally and IS throwing an exception when run on my Heroku remote dyno? In other words, why am I getting a "Success!" SMS message when the process is run locally and a "Fail!" message when it is run on my Heroku remote dyno?

I'm confused why the process is succeeding when run locally and failing when run on Heroku remote dyno. The only difference between the two runBestBuyBoy functions is how the webdriver is instantiated. So the reason for one failing and one succeeding could be due to this difference, but I'm not sure.

Also, I would like to know if anyone knows how to get a more detailed and helpful message when an exception is caught? Something more than "Message:"? I've been using str(e), but is there another way to get an exception message that will yield a more helpful message for debugging purposes?

Thanks! Please let me know if I need to clear up anything!



from Selenium Chrome Webdriver process Working Locally But Not On Heroku

No comments:

Post a Comment