2

Can someone please help me on how to open multiple instances of firefox using selenium in python.

I wrote the following code and it does open multiple instances but I would want to keep all the references to the browser so I can access each browser to do specific tasks and should be able to Quit all the browsers once the test is done.

class LoadTestCase(unittest.TestCase):

def setUp(self):
    i = 0
    while (i < 2):
      self.driver = webdriver.Firefox()
      self.driver.implicitly_wait(30)
      self.base_url = "http://example.com/"
      self.verificationErrors = []
      self.accept_next_alert = True
      driver = self.driver
      url = "http://example.com/"
      driver.get(self.base_url + "/")
      i = i + 1
1

1 Answer 1

0

For one thing:

for i in range(2):
    ...

is much neater and less error-prone than:

i = 0
while i < 2:
    ...
    i = i + 1 # or just 'i += 1'

But your main problem is that each time through the loop you replace the previous webdriver, losing access to it:

self.driver = webdriver.Firefox()

Instead, consider a list:

self.drivers = []
for i in range(2):
    self.drivers.append(webdriver.Firefox())
    ...

now you retain access to all of them.

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

1 Comment

Two things: 1. Code is very hard to read in the comments (particularly Python, where indentation matters). 2: Your description of the problem is useless. However, the issue is clear; you need to get and quit each instance in the list separately - you can't just call the method on the list and expect it to work.

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.