1.
class SentryUIChromeDriver(BaseDriver):
def __init__(self, **browser_args):
from selenium.webdriver.chrome.service import Service
try:
self.driver = Chrome(**browser_args)
except WebDriverException:
#self.driver_path = install_matching_chrome_driver()
#self.driver = Chrome(service=Service(self.driver_path), **browser_args)
self.driver = Chrome(service=Service(ChromeDriverManager().install()),**browser_args)
super().__init__(self.driver)
atexit.register(self.kill_service)
2.
from Element.Element import Element标签:return,get,self,driver,refactor,window,def From: https://www.cnblogs.com/pangniu/p/17651383.html
from Utilitys.WaitUtils import WaitUtils
class BaseDriver(Element):
def __init__(self, driver):
super().__init__(driver)
def get_driver(self):
return self
def find_element(self, by, value):
try:
try:
return self.driver.find_element(by, value)
except Exception:
self._refresh()
WaitUtils.wait_for_element_present(self.driver, by, value)
return self.driver.find_element(by, value)
except Exception:
raise Exception("Cannot find element by [%s]:[%s] under:\n %s \n" % (by, value, self))
def find_elements(self, by, value, number=1):
try:
if len(self.__find_elements(by, value)) >= number:
return self.__find_elements(by, value)
else:
raise Exception
except Exception:
self._refresh()
WaitUtils.wait_for_elements_number_right(self._appium_context(), by, value, number)
return self.__find_elements(by, value)
def __find_elements(self, by, value):
try:
try:
return self._appium_context().find_elements(by, value)
except Exception:
self._refresh()
WaitUtils.wait_for_element_present(self._appium_context(), by, value)
return self._appium_context().find_elements(by, value)
except Exception:
raise Exception("Cannot find element by [%s] under: \n %s \n" % (value, self))
def before_startup(self, method):
self.driver.start_client = method
def after_shutdown(self, method):
self.driver.stop_client = method
def start_session(self, desired_capabilities):
self.driver.start_session(desired_capabilities)
def set_page_load_timeout(self, timeout):
self.driver.set_page_load_timeout(timeout / 1000.0)
def set_script_timeout(self, time_to_wait):
self.driver.set_script_timeout(time_to_wait / 1000.0)
def execute_script(self, script, *args):
return self.driver.execute_script(script, *args)
def execute_async_script(self, script, *args):
return self.driver.execute_async_script(script, *args)
def open(self, url):
self.driver.get(url)
def get_title(self):
return self.driver.title
def get_page_source(self):
return self.driver.page_source
def refresh(self):
self.driver.refresh()
def back(self):
self.driver.back()
def forward(self):
self.driver.forward()
def quit(self):
self.driver.quit()
def get_current_url(self):
return self.driver.current_url
def switch_to_frame(self, frame_reference):
self.driver.switch_to.frame(frame_reference)
def switch_to_parent_frame(self):
self.driver.switch_to.parent_frame()
def switch_to_default_content(self):
self.driver.switch_to.default_content()
def get_alert(self):
return self.driver.switch_to.alert
def get_cookies(self):
return self.driver.get_cookies()
def get_cookie(self, name):
return self.driver.get_cookie(name)
def delete_cookie(self, name):
self.driver.delete_cookie(name)
def delete_all_cookies(self):
self.driver.delete_all_cookies()
def add_cookie(self, cookie_dict):
self.driver.add_cookie(cookie_dict)
def get_screenshot_as_file(self, filename):
return self.driver.get_screenshot_as_file(filename)
def get_current_window_handle(self):
return self.driver.current_window_handle
def get_window_handles(self):
return self.driver.window_handles
def switch_to_window(self, window_reference):
self.driver.switch_to.window(window_reference)
def close_window(self, window_reference="current"):
if window_reference == "current":
self.driver.close()
else:
current_window = self.get_current_window_handle()
self.switch_to_window(window_reference)
self.driver.close()
self.switch_to_window(current_window)
def set_window_size(self, width, height, window_reference="current"):
self.driver.set_window_size(width, height, window_reference)
def get_window_size(self, window_reference="current"):
return self.driver.get_window_size(window_reference)
def set_window_position(self, x, y, window_reference="current"):
self.driver.set_window_position(x, y, window_reference)
def get_window_position(self, window_reference="current"):
return self.driver.get_window_position(window_reference)
def get_application_cache(self):
return self.driver.application_cache
def get_log_types(self):
return self.driver.log_types
def get_log(self, log_type):
return self.driver.get_log(log_type)