1

I am trying to be able to manage multiple instances of Selenium at the same time, but haven't had much luck. I'm not 100% sure if it's possible. I have an application with a GUI built with PyQT that retrieves our client's information from our SQL database. It's a fairly simple app that lets our users easily log in and out of our clients' accounts. They click the client's name, press "Login", it launches an instance of Firefox, logs into the account, and stays open so the user can do whatever they need to do. When they are done, they click the "Logout" button, and it logs out of the account and quits the webdriver instance.

What I'm trying to provide is a way for them to log into multiple accounts at once, while still maintaining the ability to click one of the client's names that they are logged into, process the logout on that account, and close that browser instance.

One thing I was hoping is to be able to control the webdriver by either a process ID, or unique ID, in which I can store in a dictionary linking it to that client, so when they click the client's name in the app, and press logout, it uses something in PyQT like "client_name = self.list_item.currentItem().text()" to get the name of the client they have selected (which I'm already using for other things, too), finds the unique ID or process ID, and sends the logout command to that instance, and then closes that instance.

This may not be the best way to go about doing it, but it's the only thing I could think of.

EDIT: I also know that you can retrieve the Selenium session_id with driver.session_id (considering your webdriver instance is assigned as 'driver'), but i have seen nothing so far on being able to control a webdriver instance by this session_id.

EDIT2: Here is an incredibly stripped down version of what I have:

from selenium import webdriver
from PyQt4 import QtGui, QtCore


class ClientAccountManager(QtGui.QMainWindow):

    def __init__(self):

        super(ClientAccountManager, self).__init__()

        grid = QtGui.QGridLayout()
        # Creates the list box
        self.client_list = QtGui.QListWidget(self)

        # Populates the list box with owner data
        for name in client_names.itervalues():
            item = QtGui.QListWidgetItem(name)
            self.client_list.addItem(item)

        # Creates the login button
        login_btn = QtGui.QPushButton("Login", self)
        login_btn.connect(login_btn, QtCore.SIGNAL('clicked()'), self.login)

        # Creates the logout button
        logout_btn = QtGui.QPushButton("Logout", self)
        logout_btn.connect(logout_btn, QtCore.SIGNAL('clicked()'), self.logout)


    def login(self):

        # Finds the owner info based on who is selected
        client_name = self.client_list.currentItem().text()
        client_username, client_password = get_credentials(client_name)

        # Creates browser instance
        driver = webdriver.Firefox()

        # Logs in
        driver.get('https://www.....com/login.php')
        driver.find_element_by_id('userNameId').send_keys(client_username)
        driver.find_element_by_id('passwordId').send_keys(client_password)
        driver.find_element_by_css_selector('input[type=submit]').click()


    def logout(self):

        # Finds the owner info based on who is selected
        client_name = self.client_list.currentItem().text()

        # Logs out
        driver.get('https://www....com/logout.php')

        # Closes the browser instance
        driver.quit()


def main():

    app = QtGui.QApplication(sys.argv)
    cpm = ClientAccountManager()
    cpm.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
4
  • driver is a local variable in login() that you're not keeping anywhere Commented Aug 5, 2014 at 18:07
  • It's actually stored differently, but i just stripped out parts of the code i didn't see relevant. driver is stored globally Commented Aug 5, 2014 at 18:09
  • If driver is a global then you're only keeping a reference to the last driver. Put them in a dict by username or something. Commented Aug 5, 2014 at 18:37
  • @JasonS Oh geez, i didn't even think of that. Works perfect! Thanks Commented Aug 5, 2014 at 18:42

1 Answer 1

1

You can have multiple drivers. Just call webdriver.Firefox() multiple times and keep references to each driver. Some people report oddball behavior, but it basically works.

driver.close() will close the browser and does not take an id.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you give an example on how to keep the reference without losing control of it once you start another instance, and being able to communicate with it later? Every time I call a second instance, it overwrites the first instance with the second.
I'm not doing anything special, just creating 2 webdrivers and getting a page in each. Could you post a short excerpt of how you're using them?
Creating 2 webdrivers isn't a problem. The problem is being able to control the first one once the second one has been created as it overwrites the driver variable with the new instance.

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.