Package: seleniumbot
SeleniumBotThis library provides you tools that will help you create selenium bots.Installationpip install seleniumbotActionsActions are classes that will tell bot what to do. There are two types of actions,simpleactions
andnavigationactions.Actions also have some required properties. Here's the list of them:name- Action name.coefficient- The chance that this action will be executed.Simple ActionsSimple actions are the actions that do not create other actions.Navigation ActionsNavigation actions are the actions that after execution return set of new actions.ExamplesSimple ActionThis is the example on how to create a simple action. Note, that this action receives the button
that it will click to during it's executing. This button should come fromNavigationtype action.Note: This action goes into new page, where old actions may be irrelevant. For this type
of actions we need to setflush_actions = Trueclick_video.py:importtimefromselenium.webdriver.remote.webelementimportWebElementfromseleniumbotimportSeleniumClient,ActionclassClickVideo(Action):name='ClickVideoAction'coefficient=0.1flush_actions=Truebutton:WebElementdef__init__(self,button:WebElement):self.button=buttondef_execute(self,client:SeleniumClient,last_action:Action|None):self.button.click()# YouTube has different page reload logic when clicking on videos,# so we need to add some sleep timetime.sleep(10)Navigation ActionThis is example of aScrollActionwhich isNavigationaction type, that produces new actions
(ClickVideoActionin particular).scroll.py:fromselenium.webdriver.common.byimportByfromclick_videoimportClickVideofromseleniumbotimportAction,SeleniumClientclassScrollAction(Action):name='ScrollAction'coefficient=0.75def_execute(self,client:SeleniumClient,last_action:Action|None)->list[Action]:scroll_min,scroll_max=400,1500client.scroll(scroll_min,scroll_max)click_video_actions=self.__get_click_video_actions(client)actions=[*click_video_actions]returnactions@staticmethoddef__get_click_video_actions(client:SeleniumClient)->list[ClickVideo]:video_id='thumbnail'videos=client.find_elements(By.ID,video_id)click_video_action=[ClickVideo(button=video)forvideoinvideos]returnclick_video_actionStartActionIn order to tell our bot where to start we need to create anotherStartAction.StartActionis a special action that should be executed only once, at the beginning.start.pyfromseleniumbotimportAction,SeleniumClientclassStartAction(Action):name='StartAction'coefficient=0# put any number since this field is ignored for StartActiondef_execute(self,client:SeleniumClient,last_action:Action|None):client.goto('https://www.youtube.com/')Putting it all togethermain.py:fromselenium.webdriver.chrome.optionsimportOptionsfromscrollimportScrollActionfromseleniumbotimportSeleniumBotfromstartimportStartActiondefmain():# In order to test and see how bot behaves, we need to reassign options# that will not use --headless mode.options=Options()options.add_argument('window-size=1920,1080')bot=SeleniumBot(start_action=StartAction,options=options)bot.set_navigation_actions([ScrollAction])bot.start()if__name__=='__main__':main()Using proxyIn order to use proxy, just pass second type of options to SeleniumBot.fromscrollimportScrollActionfromselenium.webdriver.chrome.optionsimportOptionsfromstartimportStartActionfromseleniumbotimportSeleniumBotdefmain():# In order to test and see how bot behaves, we need to reassign options# that will not use --headless mode.options=Options()options.add_argument('window-size=1920,1080')seleniumwire_options={'proxy':{'https':'https://user:password@host:port'}}bot=SeleniumBot(start_action=StartAction,options=options,seleniumwire_options=seleniumwire_options)bot.set_navigation_actions([ScrollAction])bot.start()if__name__=='__main__':main()