Package: croco-selenium
croco-seleniumThe package providing ways to interact with Selenium Web Driver actions, such as clicking, sending keys etc.Telegram channelBug reportsActions OverviewWhen we use Selenium, it's not convenient to use WebDriverWait with its cluttered chain actions. Instead of many imports
and instances (By, WebDriverWait, expected_conditions) you can use fast and robust actions.Package's source code is made available under theMIT LicenseQuick StartYou can use actions, by the following way.fromselenium.webdriver.chrome.webdriverimportWebDriverfromcroco_seleniumimportclickdriver=WebDriver()click(driver,10,'//input[@type="submit"]')If you don't want to pass driver every using of actions, you can create an instance of ActionPerformerfromselenium.webdriver.chrome.webdriverimportWebDriverfromcroco_seleniumimportActionPerformerdriver=WebDriver()action_performer=ActionPerformer(driver)timeout=10action_performer.send_keys(timeout,'//input[@type="password"]','password')action_performer.click(timeout,'//input[@type="submit"]')One of the best ways to use actions is create an instance of ChromeDriver and perform
actions by calling methods on it. That class derived from ActionPerformer and ChromiumDriverimportosfromcroco_seleniumimportChromeDriver,Proxyproxy=Proxy(host='123.89.46.72',port=8000,username='croco',password='webDriver')project_dir=os.path.dirname(os.path.abspath(__file__))extension_paths=[os.path.join(project_dir,'extensions/metamask.crx')]driver=ChromeDriver(proxy=proxy,extension_paths=extension_paths)driver.get('https://facebook.com')driver.send_keys(15,'//input[@id="email"]','hello@world.com')Actions OverviewYou can perform the followingactions, using croco-selenium:add_cookiesclickclose_tabsget_elementget_elementsget_element_attributeget_element_textsend_keyssilent_send_keysswitch_to_another_windowswitch_to_frameswitch_to_parent_framewait_for_invisibilitywait_for_windowsAnd there are 3 usefuldecorators:handle_pop_uphandle_in_new_tabhandle_new_tabActionsadd_cookiesAdds cookies to a current page. It takes valid string containing json, list of cookies or one cookie as dictionary.fromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()cookies='{"domain":".facebook.com","expirationDate":1689249734,"httpOnly":true,"name":"c_user","path":"/","secure":true,"value":"100083466604886"}'driver.get('facebook.com')driver.add_cookies(cookies)clickClicks on element in browserfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.click(timeout,'//input[@type="submit"]')close_tabsCloses all tabs in browser. It's convenient to use, when you add extensions to your browser and their windows occur with 
starting a driver.importosfromcroco_seleniumimportChromeDriverproject_dir=os.path.dirname(os.path.abspath(__file__))extension_paths=[os.path.join(project_dir,'extensions/metamask.crx')]driver=ChromeDriver(extension_paths=extension_paths)driver.close_tabs()driver.get('https://facebook.com')driver.send_keys(15,'//input[@id="email"]','hello@world.com')get_elementReturns an element in browserfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()element=driver.get_element(timeout,'//input[@type="submit"]')element.click()get_elementsReturns an elements in browserfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()elements=driver.get_elements(timeout,'//input')forelementinelements:element.click()get_element_attributeReturns an element's attribute in browserfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()assertdriver.get_element_attribute(timeout,'//input[@type="checkbox"]','checked')get_element_textReturns element's text in browserfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()print(driver.get_element_text(timeout,'//h1'))send_keysSends keys in browserfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.send_keys(timeout,'//input[@type="password"]','password')driver.click(timeout,'//input[@type="submit"]')silent_send_keysSends keys with delay between characters in browser. It's convenient to use when you would like to hide your bot activityfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.silent_send_keys(timeout,'//input[@type="password"]','password')driver.click(timeout,'//input[@type="submit"]')switch_to_another_windowSwitches to a different window from current window in browser. It's convenient to use, when you have two windows to be handledfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.silent_send_keys(timeout,'//input[@type="password"]','password')driver.click(timeout,'//input[@type="submit"]')driver.switch_to_another_window(timeout)driver.click(timeout,'//input[@type="submit"]')driver.switch_to_another_window(timeout)switch_to_frameSwitches to the framefromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.switch_to_frame(timeout,'//iframe[@data-hcaptcha-widget-id]')driver.click(timeout,'//input[@type="submit"]')switch_to_parent_frameSwitches to the parent framefromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.switch_to_frame(timeout,'//iframe[@data-hcaptcha-widget-id]')driver.click(timeout,'//input[@type="submit"]')driver.switch_to_parent_frame()wait_for_invisibilityWait for element's invisibility in browserfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.click(timeout,'//button')driver.wait_for_invisibility(timeout,'//*[@id="popup"]')wait_for_windowsWait for occurring of number of windowsfromcroco_seleniumimportChromeDrivertimeout=10driver=ChromeDriver()driver.wait_for_windows(timeout,2)Decoratorshandle_pop_upSwitches to another window, performs decorated function and switches back. Pop up has to be closed after performing
decorated function.This decorator is usually used for browser extensions' pop-ups. Example of function performing
a third-party Metamask connection:fromcroco_seleniumimportChromeDriver,handle_pop_upfromselenium.commonimportTimeoutException@handle_pop_up()defconnect(driver:ChromeDriver,password:str)->None:try:password_xpath='//*[@id="password"]'driver.send_keys(7,password_xpath,password)unlock_xpath='//button[@data-testid="unlock-submit"]'driver.click(5,unlock_xpath)exceptTimeoutException:passfor_inrange(3):next_xpath='//button[@data-testid="page-container-footer-next"]'driver.click(10,next_xpath,ignored_exceptions=TimeoutException)handle_in_new_tabOpens new tab, performs decorated function, closes new tab and switches back. Here is example of function performing 
getting of 2FA code from browser extension.fromcroco_seleniumimportChromeDriver,handle_in_new_tab@handle_in_new_tab()defget_code(driver:ChromeDriver,account)->str:timeout=15driver.get('<EXTENSION_URL>')code_field_xpath='//*[contains(@class, "code")]'code_fields=driver.get_elements(timeout,code_field_xpath)code_field=code_fields[account.id]code=code_field.textreturncodehandle_new_tabPerforms decorated function in new tab and switches back. New tab has to be opened during performing decorated function.Installing croco-seleniumTo install the package from PyPi you can use:pipinstallcroco-seleniumTo install the package from GitHub you can use:pipinstallgit+https://github.com/blnkoff/croco-selenium.git