[splinter chromedriver在python中崩溃

问题描述 投票:0回答:1

我正在尝试在使用splinter库的mac机上使python程序正常工作。不幸的是,每当我尝试运行该程序时,该程序就会崩溃。该程序的作者:https://github.com/lrnq/supremeBot

[每当我尝试运行该程序时,它都会快速打开浏览器3次,但会立即崩溃。我收到的错误是:

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

知道为什么会这样或如何解决?

发生错误的initializeBrowser函数:

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)

我已经在终端中使用以下命令安装了库:

pip3 install splinter requests bs4

此外,chromedriver位于bin文件夹中:

/usr/local/bin/chromedriver

此外,chomedriver已添加到路径:

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

程序:

#!/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": "[email protected]",
        "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()
python selenium-chromedriver splinter
1个回答
0
投票

看起来碎片在他们的代码中有错误,导致您没有正确的错误消息。您可以在他们的问题here中指出这一点。从python2到python3,这可能与它们的某些问题有关。该答案说明了如何很好地进行here

您可以采取一些措施来尝试自己解决问题。我认为问题与您的chromedriver无法正确到达有关。

  1. 请确保您的chromedriver和Google chrome浏览器是同一版本。例如,如果您的浏览器版本为81.xxxx,请确保您的chromedriver版本也为81.xxx

  2. 将您的chromedriver放在与main.py所在的目录相同的目录中

  3. self.b = Browser('chrome', **executable_path)行更改为self.b = Browser('chrome', **executable_path, headless=False)
© www.soinside.com 2019 - 2024. All rights reserved.