1 文件名你们自己命名就好,至于为什么要重写强制位移的函数呢,是因为Mac上Selenium不支持拖拽,只能这样做了,4个文件
----------------------------------------------------------------------------------------------
def login(auto): 2 """ 3 遍历上方login_config 4 按照其格式进行自动化操作 5 """ 6 for item in login_config: 7 for key, value in item.items(): 8 try: 9 # 把auto与key组合起来并传入value执行 10 # 如auto.refresh(2) 11 getattr(auto, key)(value) 12 except Exception as error: 13 print(error) 14 def Test(auto): 15 """ 16 遍历上方login_config 17 按照其格式进行自动化操作 18 """ 19 for item in Test_config: 20 for key, value in item.items(): 21 try: 22 # 把auto与key组合起来并传入value执行 23 # 如auto.refresh(2) 24 getattr(auto, key)(value) 25 except Exception as error: 26 print(error)
----------------------------------------------------------------------------------------------
# 拖动单行文本 Test1_config = [ {"drag_and_drop": ["//*[@id=\"root\"]/div/div/div[4]/div/div/div", "/html/body/div/div/div[2]/div[3]/div[1]/div[1]/div/div[1]", 2]}, {"clear_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[6]/div[2]/span/span/span[1]/input", 2]}, {"write_in_xpath": [ "//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[6]/div[2]/span/span/span[1]/input", "supplier", 2]}, {"click_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[7]/span", 2]}, {"write_in_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[7]/span/input", "supplier_name", 2]} ] # 拖动多行文本 Test2_config = [ {"drag_and_drop1": ["//*[@id='root']/div/div[1]/div[4]/div[2]/div/div", "/html/body/div/div/div[2]/div[3]/div[1]/div[1]/div/div[1]", 2]}, {"clear_xpath": [ "//span[@class='ant-input-wrapper ant-input-group']//input[@class='ant-input' and @value='Multi-line Text']", 2]}, {"write_in_xpath": [ "//span[@class='ant-input-wrapper ant-input-group']//input[@class='ant-input' and @value='Multi-line Text']", "Notes", 2]}, {"click_xpath": ["//div[@class='bg-c-nodeproperties']/span[@class='ant-input-affix-wrapper", 2]}, {"write_in_xpath": ["//div[@class='bg-c-nodeproperties']/span[@class='ant-input-affix-wrapper']/input", "supplier_notes", 2]} ]
----------------------------------------------------------------------------------------------
import sys from selenium import webdriver from time import sleep from datetime import datetime from selenium.webdriver.common.action_chains import ActionChains from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver import ActionChains from selenium.common.exceptions import UnexpectedAlertPresentException from selenium.webdriver.common.action_chains import ActionChains import pyautogui # 重写的强制的位移函数 def move_mouse_to_element(driver, target_element): element_y_offset = int(target_element.location.get("y")) element_x_offset = int(target_element.location.get("x")) element_width = int(target_element.size.get("width")) element_height = int(target_element.size.get("height")) inner_height = int(driver.execute_script("return innerHeight")) screen_height = int(driver.execute_script("return outerHeight")) window_height = int(driver.execute_script("return window.screenY")) pyautogui.moveTo(element_x_offset + element_width / 2, element_y_offset + screen_height - inner_height - window_height + element_height / 2 + 500, duration=0.5) return target_element # 重写的强制位移的第二个函数 def move_mouse_to_element1(driver, target_element): element_y_offset = int(target_element.location.get("y")) element_x_offset = int(target_element.location.get("x")) element_width = int(target_element.size.get("width")) element_height = int(target_element.size.get("height")) inner_height = int(driver.execute_script("return innerHeight")) screen_height = int(driver.execute_script("return outerHeight")) window_height = int(driver.execute_script("return window.screenY")) pyautogui.moveTo(element_x_offset + element_width / 2 + 100, element_y_offset + screen_height - inner_height - window_height + element_height / 2, duration=0.5) return target_element # 和第一个位移函数配套用的 def drag_and_drop(driver, action, source, target): move_mouse_to_element(driver, target) action.drag_and_drop(source, target).perform() # 和第二个位移函数配套用的 def drag_and_drop1(driver, action, source, target): move_mouse_to_element1(driver, target) action.drag_and_drop(source, target).perform() # 输出的Log def write_finish_info(func): # @wraps(func) def wrapper(self, *args): func(self, *args) now = datetime.now().strftime("%X") name = "log_{}.txt".format('1') log = "[{}] 当前执行步骤为{}:{} {}\n".format(now, step, func.__name__, args) sys.stdout.write("[{}] 当前执行步骤为:{} {}\n".format(now, func.__name__, args)) with open(name, "a+") as f: f.write(log) return wrapper # 自动化所有用到的方法 class AutoTest: """ 自动化测试的主要脚本 所有行为在此类中编写 具体的操作及操作值,由配置表控制 """ def __init__(self, driver): self.driver = driver self.action = ActionChains(driver) @write_finish_info def get_url(self, entry): """ 打开网页的方法 传入必须为列表list,会检测list长度是否2 如若不为list或长度不对,则中断后续操作 若不想中断,则把raise ValueError("get_url gets a wrong value")改为return即可 """ if not isinstance(entry, list) and len(entry) != 2: raise ValueError("get_url gets a wrong value") url, s = tuple(entry) self.driver.get(url) sleep(s) @write_finish_info def maximize_window(self, s): sleep(s) self.driver.maximize_window() @write_finish_info def write_in_xpath(self, entry): """ 在xpath写入值的方法 传入必须为list,会检测list长度是否为3 """ if not isinstance(entry, list) and len(entry) != 3: raise ValueError("write_in_xpath gets a wrong value") xpath, words, s = tuple(entry) try: self.driver.find_element_by_xpath(xpath).send_keys(words) except Exception as e: print(e.error) sleep(s) @write_finish_info def write_in_id(self, entry): """ 在id写入值的方法 传入必须为list,会检测list长度是否为3 """ if not isinstance(entry, list) and len(entry) != 3: raise ValueError("write_in_id gets a wrong value") id, words, s = tuple(entry) self.driver.find_element_by_id(id).send_keys(words) sleep(s) @write_finish_info def click_xpath(self, entry): """ 点击xpath的方法 传入必须为list,会检测list长度是否为2 """ if not isinstance(entry, list) and len(entry) != 2: raise ValueError("click_xpath gets a worng value") xpath, s = tuple(entry) self.driver.find_element_by_xpath(xpath).click() sleep(s) @write_finish_info def clear_xpath(self, entry): """ 点击xpath的方法 传入必须为list,会检测list长度是否为2 """ if not isinstance(entry, list) and len(entry) != 2: raise ValueError("click_xpath gets a worng value") xpath, s = tuple(entry) self.driver.find_element_by_xpath(xpath).clear() sleep(s) @write_finish_info def refresh(self, s): """ 在等待s秒后,refresh """ sleep(s) self.driver.refresh() @write_finish_info def drag_and_drop(self, entry): if not isinstance(entry, list) and len(entry) != 4: raise ValueError("drag_and_drop gets a wrong value") action = ActionChains(self.driver) source_xpath, target_xpath, s = tuple(entry) source = self.driver.find_element_by_xpath(source_xpath) target = self.driver.find_element_by_xpath(target_xpath) move_mouse_to_element(self.driver, target) action.drag_and_drop(source, target).perform() sleep(s) @write_finish_info def drag_and_drop1(self, entry): if not isinstance(entry, list) and len(entry) != 3: raise ValueError("drag_and_drop gets a wrong value") action = ActionChains(self.driver) source_xpath, target_xpath, s = tuple(entry) source = self.driver.find_element_by_xpath(source_xpath) target = self.driver.find_element_by_xpath(target_xpath) move_mouse_to_element1(self.driver, target) action.drag_and_drop(source, target).perform() sleep(s)
----------------------------------------------------------------------------------------------
from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver import ActionChains from selenium.common.exceptions import UnexpectedAlertPresentException from selenium.webdriver.common.action_chains import ActionChains from Login import * import time driver = webdriver.Chrome(executable_path="/Users/jerry/PycharmProjects/untitled/venv/Goole/chromedriver") auto = AutoTest(driver) while True: step = 0 login(auto, step)
有啥疑问欢迎提出,共同进步
by-咯咯