赞
踩
将Selenium的主要操作封装成class,方便后续调用。
- import os, time, faker, random
- from typing import Tuple, Union, Dict
- from selenium.webdriver import Chrome
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium.webdriver.support import expected_conditions as when, expected_conditions
- from selenium.webdriver import ActionChains, Keys
-
-
- class Page:
-
- def __init__(self, driver: Chrome):
- self.driver = driver
-
- def goto(self, url):
- """ 去哪个url地址 """
- # if url.find("https://") != -1:
- # return self.browser.get(url)
- return self.driver.get(url)
-
- def input(self, locator, values):
- """ 输入框当中输入内容 """
- el = self.driver.find_element(*locator)
- el.send_keys(values)
-
- def clear(self, element):
- """ 清空输入框中的内容 """
- el = self.driver.find_element(*element)
- el.clear()
-
- def click(self, locator):
- """ 点击 """
- # 设置显性等待时间
- wait = WebDriverWait(self.driver, timeout=8)
- # 等待某个元素出现并可点击
- condition = when.element_to_be_clickable(locator)
- # 定位元素.点击按钮
- element = wait.until(condition)
- # 点击元素
- ActionChains(self.driver).click(element).perform()
-
- def double_click(self, locator):
- """ 双击 """
- # 设置显性等待时间
- wait = WebDriverWait(self.driver, timeout=10)
- # 等待某个元素出现并可点击
- condition = when.element_to_be_clickable(locator)
- # 定位元素.点击按钮
- element = wait.until(condition)
- # 双击元素
- ActionChains(self.driver).double_click(element).perform()
-
- def drag_and_drop(self, locator1, locator2):
- """ 拖动 """
- # 设置显性等待时间
- wait = WebDriverWait(self.driver, timeout=10)
- condition = when.element_to_be_clickable(locator1)
- element1 = wait.until(condition)
- # 定位到元素1,定位到元素2
- condition = when.element_to_be_clickable(locator2)
- element2 = wait.until(condition)
- # 拖动元素
- ActionChains(self.driver).drag_and_drop(element1, element2).perform()
-
- def is_element_exist(self, element: Tuple[str, Union[str, Dict]], wait_seconds: int = 10) -> bool:
- """ 判断元素是否存在 """
- by = element[0]
- value = element[1]
-
- try:
- if by == "id":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.ID, value)))
- elif by == "name":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.NAME, value)))
- elif by == "class":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, value)))
- elif by == "text":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.LINK_TEXT, value)))
- elif by == "partial_text":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, value)))
- elif by == "xpath":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.XPATH, value)))
- elif by == "css":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, value)))
- elif by == "tag":
- WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.TAG_NAME, value)))
- else:
- raise NameError("Please enter the correct targeting elements,'id','name','class','text','xpath','css'.")
- except:
- return False
- return True
-
- def get_element_text(self, locator):
- """ 元素定位,获取text文本 """
- el = self.driver.find_element(*locator)
- return el.text
-
- def is_text_exist(self, text: str, wait_seconds: int = 10) -> bool:
- """ 判断text是否于当前页面存在 """
- for i in range(wait_seconds):
- if text in self.driver.page_source:
- return True
- time.sleep(1)
- return False
-
- def is_loaded(self, url, timeout=8):
- """ 判断某个 url 是否被加载 """
- return WebDriverWait(self.driver, timeout).until(when.url_contains(url))
-
- def get_element_attribute(self, locator, expected):
- """ 获取某个元素的属性 """
- el = self.driver.find_element(*locator)
- return el.get_attribute(expected)
-
- def get_toast_text(self):
- """ toast弹窗/获取toast文本内容 """
- toast = self.driver.find_element("xpath", "//android.widget.Toast")
- return toast.text
-
- def emter(self):
- """ 回车 """
- return ActionChains(self.driver).send_keys(Keys.ENTER).perform()
-
- def copy(self):
- """ 复制快捷键 """
- actions = ActionChains(self.driver)
- return actions.key_down(Keys.CONTROL).send_keys("c").key_up(Keys.CONTROL).perform()
-
- def paste(self):
- """ 粘贴的快捷键 """
- actions = ActionChains(self.driver)
- return actions.key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()
-
- def set_attribute(self, locator, name, value):
- """ 设置元素属性(12306) """
- el = self.driver.find_element(*locator)
- js = f'arguments[0].{name} = f"{value}"'
- self.driver.execute_script(js,el)
-
- def switch_to_iframe(self, locator, timeout=10):
- """ 切换iframe """
- # iframe = self.browser.find_element(*locator)
- # self.browser.switch_to.frame(iframe)
- # 使用显性等待 切换 iframe
- wait = WebDriverWait(self.driver, timeout)
- wait.until(when.frame_to_be_available_and_switch_to_it(locator))
-
- def switch_to_default(self):
- """ iframe 切回主页面 """
- self.driver.switch_to.default_content()
-
- def scroll_to_bottom(self):
- """ 滚动到页面底部,使用js操作 """
- js = "window.scrollTo(0,document.body.scrollHeight)"
- self.driver.execute_script(js)
-
- def screenshot(self, name):
- """ 截图(注释的部分,根据个人需求可增or减) """
- # day = time.strftime('%Y-%m-%d', time.localtime(time.time()))
- # fp = "..\\Result\\" + day
- fp = ".\\images\\" # ".":表示上级; "..":表示上上级
-
- tm = time.strftime('%Y-%m-%d-%H_%M', time.localtime(time.time()))
- if os.path.exists(fp):
- filename = fp + "\\" + tm + '_' + name + '.png'
- else:
- os.makedirs(fp)
- filename = fp + "\\" + tm + '_' + name + '.png'
- self.driver.save_screenshot(filename)
-
- def randmon_phone(self):
- """ 随机生成一个手机号,或者其他想生成的数据 """
- while True:
- phone = "130"
- for i in range(8):
- num = random.randint(0, 9)
- phone += str(num)
- return phone
-
- def generate_phone_number(self):
- """ 随机生成手机号(与上面的实现方法一致,写法用了列表推导式) """
- prefix = "130"
- suffix = [random.randint(0, 9) for _ in range(8)]
- return f"{prefix}{''.join([str(i) for i in suffix])}"
-
- def new_mobile(self):
- """ 随机生成手机号,需下载:pip install pytest_facker """
- fk = faker.Faker(locale=["zh_CN"])
- return fk.phone_number()
-
-
- if __name__ == '__main__':
- pass
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。