Mojtaba Ghanidel
Mojtaba Ghanidel

Reputation: 27

Open a browser in Python

I have a list of 3 browsers, with the random choice I want to open a link randomly using one of the browsers in my list, but when I run the code, all browsers get opened.

my_url="URL"
dlist=[webdriver.Edge(),webdriver.Chrome(),webdriver.Firefox()]
web_driver = random.choice(dlist)
web_driver.get(url)
web_driver.quit()

Upvotes: 0

Views: 73

Answers (2)

Thomas Weller
Thomas Weller

Reputation: 59218

Since all of the entries in the list have parenthenses, an object will be created immediately, thus opening each browser.

Remove the parenthenses to have the class in the list, not an object.

dlist=[webdriver.Edge , webdriver.Chrome , webdriver.Firefox ]
                     ^                  ^                   ^ do not create an object

Then, create the object later after choosing the class

web_driver = random.choice(dlist)()
                                 ^^ create the object

Upvotes: 2

Sayse
Sayse

Reputation: 43300

You're initalizing them all inside the list, just keep a list of the browsers then initialize them when selected

dlist=[webdriver.Edge,webdriver.Chrome,webdriver.Firefox]
web_driver = random.choice(dlist)()

Upvotes: 3

Related Questions