简体   繁体   English

python 中的分裂 chromedriver 崩溃

[英]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.我正在尝试使 python 程序在我的 Mac 机器上运行,其中正在使用 splinter 库。 Unfortunately the program crashes, whenever i attempt to run the program.不幸的是,每当我尝试运行程序时,程序就会崩溃。 Author of the program: https://github.com/lrnq/supremeBot程序作者: https://github.com/lrnq/supremeBot

Whenever i attempt to run the program, it quickly opens a browser 3 times, but crashes immediately.每当我尝试运行该程序时,它会快速打开浏览器 3 次,但立即崩溃。 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: initializeBrowser function 出现错误的地方:

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:此外,chromedriver 位于 bin 文件夹中:

/usr/local/bin/chromedriver

In addition, chomedriver has been added to path:另外,在 path 中添加了 chomedriver:

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.看起来 splinter 在他们的代码中有一个错误,导致您没有正确的错误消息。 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.这可能与从 python2 到 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.我认为问题与您的 chromedriver 未正确访问有关。

  1. Make sure your chromedriver and your google chrome browser are the same version.确保您的 chromedriver 和您的 google chrome 浏览器版本相同。 For example, if you have a browser version of 81.xxxx make sure your chromedriver is also of version 81.xxx例如,如果您的浏览器版本为 81.xxxx,请确保您的 chromedriver 也是 81.xxx 版本

  2. Put your chromedriver in the same directory as from where your main.py is called将您的 chromedriver 放在与调用 main.py 的目录相同的目录中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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