1

I have two classes, one uses selenium to browse the internet, and the other inserts data into a mysql database.

class DBConnection:
    def __init__(self, db_host=DB_HOST, db_port=DB_PORT, db_user=DB_USER, db_password=DB_PASSWORD, db_name=DB_NAME):
        self.host = DB_HOST
        self.port = DB_PORT
        self.name = DB_NAME
        self.user = DB_USER
        self.password = DB_PASSWORD
        self.conn = None
        self.cursor = None

class WebDriver:
    def __init__(self, download_folder=DOWNLOAD_FOLDER):
        self.driver = False

    def start_webdriver(self):
        """
        Initiate a Firefox profile for Selenium WebDriver.
        """
        fp = webdriver.FirefoxProfile()
        self.driver = webdriver.Firefox(firefox_profile=fp)

What would be the best way to get a db connection from the WebDriver class? Would something like this work?

class WebDriver:
    def __init__(self, download_folder=DOWNLOAD_FOLDER):
        self.driver = False
        self.download_folder = download_folder
        self.last_payment = None
        self.DBConnection = DBConnection()
        self.DBConnection.get_conn()
        self.DBConnection.get_cursor()

1 Answer 1

2

For starts, you are initializing DBConnection wrong! Should be

class DBConnection(object):
    def __init__(self, db_host=DB_HOST, db_port=DB_PORT, db_user=DB_USER, db_password=DB_PASSWORD, db_name=DB_NAME):
        self.host = db_host    # lower case!
        self.port = db_port
        self.name = db_name
        self.user = db_user
        self.password = db_password

then you can allow the user to specify settings like so:

class WebDriver(object):
    def __init__(self, download_folder=DOWNLOAD_FOLDER, **kwargs):
        self.driver = False
        self.download_folder = download_folder
        self.last_payment = None
        self.DBConnection = DBConnection(**kwargs)
        self.DBConnection.get_conn()
        self.DBConnection.get_cursor()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, what would be an example of a kwarg that a user might pass to the DBConnection?
wd = WebDriver(db_host='192.168.0.5', db_port=3401)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.