0

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()

2 Answers 2

3

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)()
Sign up to request clarification or add additional context in comments.

Comments

2

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

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.