python3.8+selniume二次封装

  • Post author:
  • Post category:python



基类的二次封装selenium


描述:selenium 基础类的二次封装selenium


from selenium.webdriver.common.action_chains import ActionChains


from selenium.webdriver.support.select import Select


from selenium import webdriver


import time


from selenium.webdriver.common.by import By


class Base(object):


def __init__(self):



“””



忽略一些log



“””




options = webdriver.ChromeOptions()


options.add_experimental_option(“excludeSwitches”, [“enable-logging”])


driver = webdriver.Chrome(options=options)


self.driver = driver


def clear_cookies(self):



“””



清除cookie



“””




self.driver.delete_all_cookies()


def refresh_driver(self):



“””



刷写当前页面



“””




self.driver.refresh()


def back_page(self):



“””



返回



“””




self.driver.back()


def maxsize_window(self):



“””



窗口最大化



“””




self.driver.maximize_window()


def open_url(self, url):



“””



打开网页



“””




self.driver.get(url)


def quite_driver(self):



“””



退出浏览器



“””




self.driver.quit()


def close_current_page(self):



“””



关闭当前页面



“””




self.driver.close()


def get_element(self, selector):



“””



获取元素,通过,区分开,类别 内容



(‘x,//*[@id=”kw”])



“””




if “,” not in selector:


return self.driver.find_element(By.ID, value=selector)


selector_by = selector.split(“,”)[0]


selector_value = selector.split(“,”)[1]


if selector_by == “i” or selector_by == “id”:


w_element = self.driver.find_element(By.ID, value=selector_value)


elif selector_by == “n” or selector_by == “name”:


w_element = self.driver.find_element(By.NAME, value=selector_value)


elif selector_by == “c” or selector_by == “class_name”:


w_element = self.driver.find_element(By.CLASS_NAME, value=selector_value)


elif selector_by == “l” or selector_by == “link_text”:


w_element = self.driver.find_element(By.LINK_TEXT, value=selector_value)


elif selector_by == “p” or selector_by == “partial_link”:


w_element = self.driver.find_element(By.PARTIAL_LINK_TEXT, value=selector_value)


elif selector_by == “t” or selector_by == “tag_name”:


w_element = self.driver.find_element(By.TAG_NAME, value=selector_value)


elif selector_by == “x” or selector_by == “by_xpath”:


w_element = self.driver.find_element(By.XPATH, value=selector_value)


elif selector_by == “s” or selector_by == “css_selector”:


w_element = self.driver.find_element(By.CSS_SELECTOR, value=selector_value)


else:


return None


time.sleep(0.5)


return w_element


def get_elements(self, selector):



“””



获取元素列表,通过,区分开,类别 内容



“””




if “,” not in selector:


return self.driver.find_elements(By.ID, value=selector)


selector_by = selector.split(“,”)[0]


selector_value = selector.split(“,”)[1]


if selector_by == “i” or selector_by == “id”:


elements = self.driver.find_elements(By.ID, value=selector_value)


elif selector_by == “n” or selector_by == “name”:


elements = self.driver.find_elements(By.NAME, value=selector_value)


elif selector_by == “c” or selector_by == “class_name”:


elements = self.driver.find_elements(By.CLASS_NAME, value=selector_value)


elif selector_by == “l” or selector_by == “link_text”:


elements = self.driver.find_elements(By.LINK_TEXT, value=selector_value)


elif selector_by == “p” or selector_by == “partial_link”:


elements = self.driver.find_elements(By.PARTIAL_LINK_TEXT, value=selector_value)


elif selector_by == “t” or selector_by == “tag_name”:


elements = self.driver.find_elements(By.TAG_NAME, value=selector_value)


elif selector_by == “x” or selector_by == “by_xpath”:


elements = self.driver.find_elements(By.XPATH, value=selector_value)


elif selector_by == “s” or selector_by == “css_selector”:


elements = self.driver.find_elements(By.CSS_SELECTOR, value=selector_value)


else:


return None


time.sleep(0.5)


return elements


def get_element_form_parent_by_e(self, par_e_or_pare_selector, son_selector):



“””



par_e element



son_selector 文本



“,” 不包含的就默认是传进来的是id



“””




if isinstance(par_e_or_pare_selector, str):


par_e = self.get_element(par_e_or_pare_selector)


else:


par_e = par_e_or_pare_selector


if “,” not in son_selector:


return par_e.driver.find_element(By.ID, value=son_selector)


selector_by = son_selector.split(“,”)[0]


selector_value = son_selector.split(“,”)[1]


if selector_by == “i” or selector_by == “id”:


element = par_e.driver.driver.find_element(By.ID, value=son_selector)


elif selector_by == “n” or selector_by == “name”:


element = par_e.driver.find_element(By.NAME, value=selector_value)


elif selector_by == “c” or selector_by == “class_name”:


element = par_e.driver.find_element(By.CLASS_NAME, value=selector_value)


elif selector_by == “l” or selector_by == “link_text”:


element = par_e.driver.find_element(By.LINK_TEXT, value=selector_value)


elif selector_by == “p” or selector_by == “partial_link”:


element = par_e.driver.find_element(By.PARTIAL_LINK_TEXT, value=selector_value)


elif selector_by == “t” or selector_by == “tag_name”:


element = par_e.driver.find_element(By.TAG_NAME, value=selector_value)


elif selector_by == “x” or selector_by == “by_xpath”:


element = par_e.driver.find_element(By.XPATH, value=selector_value)


elif selector_by == “s” or selector_by == “css_selector”:


element = par_e.driver.find_element(By.CSS_SELECTOR, value=selector_value)


else:


return None


time.sleep(0.5)


return element


def get_elements_form_parent_by_e(self, par_e_or_pare_selector, son_selector):



“””



通过父类去查找子元素的列表



“””




if isinstance(par_e_or_pare_selector, str):


par_e = self.get_element(par_e_or_pare_selector)


else:


par_e = par_e_or_pare_selector


if “,” not in son_selector:


return par_e.driver.find_elements(By.ID, value=son_selector)


selector_by = son_selector.split(“,”)[0]


selector_value = son_selector.split(“,”)[1]


if selector_by == “i” or selector_by == “id”:


element_s = par_e.driver.driver.find_elements(By.ID, value=son_selector)


elif selector_by == “n” or selector_by == “name”:


element_s = par_e.driver.find_elementS(By.NAME, value=selector_value)


elif selector_by == “c” or selector_by == “class_name”:


element_s = par_e.driver.find_elementS(By.CLASS_NAME, value=selector_value)


elif selector_by == “l” or selector_by == “link_text”:


element_s = par_e.driver.find_elementS(By.LINK_TEXT, value=selector_value)


elif selector_by == “p” or selector_by == “partial_link”:


element_s = par_e.driver.find_elementS(By.PARTIAL_LINK_TEXT, value=selector_value)


elif selector_by == “t” or selector_by == “tag_name”:


element_s = par_e.driver.find_elementS(By.TAG_NAME, value=selector_value)


elif selector_by == “x” or selector_by == “by_xpath”:


element_s = par_e.driver.find_elementS(By.XPATH, value=selector_value)


elif selector_by == “s” or selector_by == “css_selector”:


element_s = par_e.driver.find_elementS(By.CSS_SELECTOR, value=selector_value)


else:


return None


time.sleep(0.5)


return element_s


def element_exist(self, selector):



“””



判断元素是否存在



“””




try:


self.get_element(selector)


return True


except Exception as e:


return False


def input_text(self, selector, text_info):



“””



在编辑框元素输入文本,清除原有内容之后,在输入



“””




e1 = self.get_element(selector)


e1.clear()


e1.send_keys(text_info)


def input_text_no_clear(self, selector, text_info):



“””



在编辑框元素输入文本,不清除原有内容



“””




e1 = self.get_element(selector)


e1.send_keys(text_info)


def select_by_index(self, selector, index_num):



“””



下拉框,通过index 来选择



“””




e1 = self.get_element(selector)


Select(e1).select_by_index(index_num)


def select_by_value(self, selector, value_info):



“””



下拉框,通过value 来选择



“””




e1 = self.get_element(selector)


Select(e1).select_by_value(value_info)


def get_select_value(self, selector):



“””



# 获取下拉列表的选项内容所有的



“””




value_list = []


e1 = self.get_element(selector)


op_list = e1.find_elements(By.TAG_NAME, “option”)


for option in op_list:


value_s = option.get_attribute(“text”)


value_list.append(value_s)


return value_list


def scroll_to_view(self, selector):



“””



滚动到元素



“””




e1 = self.get_element(selector)


self.driver.execute_script(‘arguments[0].scrollIntoView(false);’, e1)


def get_attribute_info(self, selector, attribute_kind):



“””



获取元素的信息,



“””




e1 = self.get_element(selector)


r_a = e1.get_attribute(attribute_kind)


return r_a


@staticmethod


def get_element_attribute_info(element, attribute_kind):



“””



获取元素的信息,



“””




r_a = element.get_attribute(attribute_kind)


return r_a


def get_title(self):



“””



获取当前页面的title



“””




return self.driver.title


def get_current_url(self):



“””



获取当前页面的url



“””




page_url = self.driver.current_url


return page_url


def switch_to_frame(self, selector):



“””



嵌套的frame ,切换到指定iframe



“””




e1 = self.get_element(selector)


self.driver.switch_to.frame(e1)


def switch_to_frame_by_handle(self, handle):



“””



嵌套的frame ,通过handle



“””




self.driver.switch_to.frame(handle)


def switch_to_default_frame(self):



“””



切换到默认的 frame



“””




self.driver.switch_to.default_content()


def switch_to_window(self, handle):



“””



切换窗口,通过 handle



“””




self.driver.switch_to.window(handle)


def get_all_handles(self):



“””



获取所有串口的handles



“””




all_handles = self.driver.window_handles


return all_handles


def get_current_handle(self):



“””



获取当前页面的handle



“””




return self.driver.current_window_handle


def switch_to_new_close_other(self, target_handle):



“””



关闭当前页面的其他页面



“””




all_handles = self.driver.window_handles


if target_handle in all_handles:


for one_h in all_handles:


if one_h != target_handle:


self.switch_to_window(one_h)


self.close_brower()


self.switch_to_window(target_handle)


def wait_element_appear(self, selector, wait_time=10):



“””



等待元素出现



“””




time.sleep(1)


flag = True


while self.element_exist(selector):


time.sleep(1)


wait_time -= 1


if wait_time == 0:


flag = False


break


return flag


def click_on_text(self, text_info):



“””



点击文本,唯一性



“””




e1 = self.get_element(‘x,//*[text=”‘ + text_info + ‘”]’)


e1.click()


def click_on_element(self, selector):



“””



点击元素



“””




e1 = self.get_element(selector)


e1.click()


def right_click(self, selector):



“””



鼠标右键点击



“””




e1 = self.get_element(selector)


ActionChains(self.driver).context_click(e1).perform()


def left_click(self, selector):



“””



鼠标左键点击



“””




e1 = self.get_element(selector)


ActionChains(self.driver).click(e1).perform()


def move_mouse_to_element_from_tag(self, selector):



“””



获取元素属性,鼠标移动至元素



“””




e1 = self.get_element(selector)


ActionChains(self.driver).move_to_element(e1).perform()


def move_mouse_to_element(self, element):



“””



鼠标移动元素上



“””




ActionChains(self.driver).move_to_element(element).perform()


def get_page_source(self):



“””



获取当前页面的source



“””




html_txt = self.driver.page_source


return html_txt


def get_default_select_value(self, selector):


e1 = Select(self.get_element(selector)).first_selected_option


select_text = e1.get_attribute(‘text’)


return select_text


def get_select_values(self, selector):



“””



获取下拉框的全部值



“””




name_list = []


e1_all = Select(self.get_element(selector)).options


for e1 in e1_all:


select_txt = e1.get_attribute(“text”)


name_list.append(select_txt)


return name_list


def take_screen_shot(self, file_name):



“””



page 页面截图操作



“””




self.driver.save_screenshot(file_name)


版权声明:本文为CSDN博主「DO?OK:NOT」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。


原文链接:



https://blog.csdn.net/weixin_43444734/article/details/124401564