1

Here I have an object in which I instantiate several Selenium WebDrivers to control a series of browsers from Selenium Grid.

drivers = {
    0: webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()),
    1: webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()), 
    2: webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()),
    3: webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()),
    4: webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy())
}

What I want to then do is pass those drivers by reference to another method. Like so:

for driver in drivers:
    self.create_account(driver)

However I get the following error from Selenium when I get into the create_accounts method:

driver.get("http://google.com")
AttributeError: 'int' object has no attribute 'get'

I assume this is because I'm not passing the object reference properly, and its somehow transferring as an integer as opposed to an object with methods I can call. Is what I'm attempting to do possible in Python? Is there some other way that I should be doing this, or is what I am attempting to do simply not possible?

2 Answers 2

2

You are creating a dictionary of drivers. If you are just going to assign each driver to a number, its probably better to create an array of drivers:

drivers = [
    webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()),
    webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()), 
    webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()),
    webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy()),
    webdriver.Remote('http://localhost:8080/wd/hub' , webdriver.DesiredCapabilities.FIREFOX.copy())
]

Then you can do:

for driver in drivers:

and

driver[4]

However, if you are going to use a dictionary, then you can do

for key, driver in drivers.items() 

if you want both the number and the driver, or

for driver in drivers.values()

if you just need the driver.

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

Comments

0

You should be iterating over drivers.values() (or drivers.itervalues() in Python 2.x):

for driver in drivers.values():

This will assign driver to the webdriver.Remote instances inside the dictionary, all of which have a get method. Your current code however is assigning driver to the dictionary's integer keys because iterating over a dictionary yields its keys.

Below is a demonstration of what I said above:

>>> dct = {'a':1, 'b':2, 'c':3}
>>> for i in dct:  # Yields keys
...     i
...
'a'
'b'
'c'
>>> for i in dct.values():  # Yields values
...     i
...
1
2
3
>>>

Comments

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.