简体   繁体   中英

splinter chromedriver crash in python

I am trying to make a python program work on my mac machine, where the splinter library is being used. Unfortunately the program crashes, whenever i attempt to run the program. Author of the program: https://github.com/lrnq/supremeBot

Whenever i attempt to run the program, it quickly opens a browser 3 times, but crashes immediately. The error I receive is:

Traceback (most recent call last):
  File "/Users/yusuf/PycharmProjects/supremeBot/supremeBot/main.py", line 113, in <module>
    BOT.initializeBrowser()
  File "/Users/yusuf/PycharmProjects/supremeBot/supremeBot/main.py", line 25, in initializeBrowser
    self.b = Browser('chrome', **executable_path)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/splinter/browser.py", line 90, in Browser
    return get_driver(driver, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/splinter/browser.py", line 68, in get_driver
    raise e
UnboundLocalError: local variable 'e' referenced before assignment

Any idea why this is happening or how to fix this?

initializeBrowser function where the error occurs:

def initializeBrowser(self):
    driver = self.info["driver"]
    #path = helpers.get_driver_path(driver)

    print(driver)

    if driver == "geckodriver":
        self.b = Browser()
    elif driver == "chromedriver":
        executable_path = {"executable_path": "/usr/local/bin/chromedriver"}
        self.b = Browser('chrome', **executable_path)

I have installed the libraries with following command in terminal:

pip3 install splinter requests bs4

Also, the chromedriver is in the bin folder:

/usr/local/bin/chromedriver

In addition, chomedriver has been added to path:

sudo nano /etc/paths: `/usr/local/bin/chromedriver`

PROGRAM:

#!/usr/bin/env python3
import requests
import bs4 as bs
from splinter import Browser
#import helpers


class supremeBot(object):
    def __init__(self, **info):
        self.base_url = 'http://www.supremenewyork.com/'
        self.shop = 'shop/all/'
        self.checkout = 'checkout/'
        self.info = info

    def initializeBrowser(self):
        driver = self.info["driver"]
        #path = helpers.get_driver_path(driver)

        print(driver)

        if driver == "geckodriver":
            self.b = Browser()
        elif driver == "chromedriver":
            executable_path = {"executable_path": "/usr/local/bin/chromedriver"}
            self.b = Browser('chrome', **executable_path)


    def findProduct(self):
        try:
            r = requests.get(
                "{}{}{}".format(
                    self.base_url,
                    self.shop,
                    self.info['category'])).text
            soup = bs.BeautifulSoup(r, 'lxml')

            temp_tuple = []
            temp_link = []

            for link in soup.find_all('a', href=True):
                temp_tuple.append((link['href'], link.text))
            for i in temp_tuple:
                if i[1] == self.info['product'] or i[1] == self.info['color']:
                    temp_link.append(i[0])

            self.final_link = list(
                set([x for x in temp_link if temp_link.count(x) == 2]))[0]
            return True
        except:
            return False

    def visitSite(self):
        self.b.visit(
            "{}{}".format(
                self.base_url, str(
                    self.final_link)))
        self.b.find_option_by_text(self.info['size']).click()
        self.b.find_by_value('add to basket').click()

    def checkoutFunc(self):

        self.b.visit("{}{}".format(self.base_url, self.checkout))

        self.b.fill("order[billing_name]", self.info['namefield'])
        self.b.fill("order[email]", self.info['emailfield'])
        self.b.fill("order[tel]", self.info['phonefield'])

        self.b.fill("order[billing_address]", self.info['addressfield'])
        self.b.fill("order[billing_city]", self.info['city'])
        self.b.fill("order[billing_zip]", self.info['zip'])
        self.b.select("order[billing_country]", self.info['country'])

        self.b.select("credit_card[type]", self.info['card'])
        self.b.fill("credit_card[cnb]", self.info['number'])
        self.b.select("credit_card[month]", self.info['month'])
        self.b.select("credit_card[year]", self.info['year'])
        self.b.fill("credit_card[ovv]", self.info['ccv'])
        self.b.find_by_css('.terms').click()
        #self.b.find_by_value("process payment").click()


if __name__ == "__main__":
    INFO = {
        "driver": "chromedriver",
        "product": "Raglan Court Jacket",
        "color": "Pale Yellow",
        "size": "Large",
        "category": "jackets",
        "namefield": "example",
        "emailfield": "example@example.com",
        "phonefield": "XXXXXXXXXX",
        "addressfield": "example road",
        "city": "example",
        "zip": "72046",
        "country": "GB",
        "card": "visa",
        "number": "1234123412341234",
        "month": "09",
        "year": "2020",
        "ccv": "123"
    }
    BOT = supremeBot(**INFO)
    # Flag to set to true if you want to reload the page continously close to drop.
    found_product = False
    max_iter = 10
    counter = 1
    while not found_product and counter < max_iter:
        found_product = BOT.findProduct()
        print("Tried ", counter, " times")
        counter += 1
    if not found_product:
        raise Exception("Couldn't find product. Sry bruh")
    BOT.initializeBrowser()
    BOT.visitSite()
    BOT.checkoutFunc()

It looks like splinter has a mistake in their code that results in you not having the correct error message. You could point this out in their issues here . It probably has to do with some problems on their end with going from python2 to python3. This answer explains how this can happen nicely here .

There are a few things you can do to try to solve the problem yourself. I think the problem has to do with your chromedriver not being reached correctly.

  1. Make sure your chromedriver and your google chrome browser are the same version. For example, if you have a browser version of 81.xxxx make sure your chromedriver is also of version 81.xxx

  2. Put your chromedriver in the same directory as from where your main.py is called

  3. Change the line self.b = Browser('chrome', **executable_path) to self.b = Browser('chrome', **executable_path, headless=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM