简体   繁体   中英

python selenium choose random firefox profile

In my script folder I have copied my firefox profiles folders

My code

#Creating profile for browser
profile = webdriver.FirefoxProfile('.\profiles\profile1')
profile.set_preference("general.useragent.override", user_agent)
profile.update_preferences()

What I would like to do is

#Creating profile for browser
profile = webdriver.FirefoxProfile('.\profiles\random_profile_from_profiles_folder')
profile.set_preference("general.useragent.override", user_agent)
profile.update_preferences()

The only modification I perform is 1) list profiles directory, 2) filter to keep only directories 3) apply random on the list

I hope it's what you want:

import os,random

profile_dir="profiles"

# pick a directory
randdir = random.choice(list(filter(os.path.isdir,os.listdir(profile_dir))))

profile = webdriver.FirefoxProfile(os.path.join(profile_dir,randdir))
profile.set_preference("general.useragent.override", user_agent)
profile.update_preferences()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM