1. 文件名你们自己命名就好,至于为什么要重写强制位移的函数呢,是因为MacSelenium不支持拖拽,只能这样做了,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)
  1. ----------------------------------------------------------------------------------------------
  1. # 拖动单行文本
  2. Test1_config = [
  3. {"drag_and_drop": ["//*[@id=\"root\"]/div/div/div[4]/div/div/div",
  4. "/html/body/div/div/div[2]/div[3]/div[1]/div[1]/div/div[1]", 2]},
  5. {"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",
  6. 2]},
  7. {"write_in_xpath": [
  8. "//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[6]/div[2]/span/span/span[1]/input", "supplier",
  9. 2]},
  10. {"click_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[7]/span", 2]},
  11. {"write_in_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[7]/span/input", "supplier_name",
  12. 2]}
  13. ]
  14. # 拖动多行文本
  15. Test2_config = [
  16. {"drag_and_drop1": ["//*[@id='root']/div/div[1]/div[4]/div[2]/div/div",
  17. "/html/body/div/div/div[2]/div[3]/div[1]/div[1]/div/div[1]", 2]},
  18. {"clear_xpath": [
  19. "//span[@class='ant-input-wrapper ant-input-group']//input[@class='ant-input' and @value='Multi-line Text']",
  20. 2]},
  21. {"write_in_xpath": [
  22. "//span[@class='ant-input-wrapper ant-input-group']//input[@class='ant-input' and @value='Multi-line Text']",
  23. "Notes",
  24. 2]},
  25. {"click_xpath": ["//div[@class='bg-c-nodeproperties']/span[@class='ant-input-affix-wrapper", 2]},
  26. {"write_in_xpath": ["//div[@class='bg-c-nodeproperties']/span[@class='ant-input-affix-wrapper']/input",
  27. "supplier_notes",
  28. 2]}
  29. ]
  1. ----------------------------------------------------------------------------------------------
  1. import sys
  2. from selenium import webdriver
  3. from time import sleep
  4. from datetime import datetime
  5. from selenium.webdriver.common.action_chains import ActionChains
  6. from selenium import webdriver
  7. from selenium.webdriver.common.keys import Keys
  8. from selenium.webdriver import ActionChains
  9. from selenium.common.exceptions import UnexpectedAlertPresentException
  10. from selenium.webdriver.common.action_chains import ActionChains
  11. import pyautogui
  12.  
  13. # 重写的强制的位移函数
  14. def move_mouse_to_element(driver, target_element):
  15. element_y_offset = int(target_element.location.get("y"))
  16. element_x_offset = int(target_element.location.get("x"))
  17. element_width = int(target_element.size.get("width"))
  18. element_height = int(target_element.size.get("height"))
  19. inner_height = int(driver.execute_script("return innerHeight"))
  20. screen_height = int(driver.execute_script("return outerHeight"))
  21. window_height = int(driver.execute_script("return window.screenY"))
  22. pyautogui.moveTo(element_x_offset + element_width / 2,
  23. element_y_offset + screen_height - inner_height - window_height + element_height / 2 + 500,
  24. duration=0.5)
  25.  
  26. return target_element
  27.  
  28. # 重写的强制位移的第二个函数
  29. def move_mouse_to_element1(driver, target_element):
  30. element_y_offset = int(target_element.location.get("y"))
  31. element_x_offset = int(target_element.location.get("x"))
  32. element_width = int(target_element.size.get("width"))
  33. element_height = int(target_element.size.get("height"))
  34. inner_height = int(driver.execute_script("return innerHeight"))
  35. screen_height = int(driver.execute_script("return outerHeight"))
  36. window_height = int(driver.execute_script("return window.screenY"))
  37. pyautogui.moveTo(element_x_offset + element_width / 2 + 100,
  38. element_y_offset + screen_height - inner_height - window_height + element_height / 2,
  39. duration=0.5)
  40.  
  41. return target_element
  42.  
  43. # 和第一个位移函数配套用的
  44. def drag_and_drop(driver, action, source, target):
  45. move_mouse_to_element(driver, target)
  46. action.drag_and_drop(source, target).perform()
  47.  
  48. # 和第二个位移函数配套用的
  49. def drag_and_drop1(driver, action, source, target):
  50. move_mouse_to_element1(driver, target)
  51. action.drag_and_drop(source, target).perform()
  52.  
  53. # 输出的Log
  54. def write_finish_info(func):
  55. # @wraps(func)
  56. def wrapper(self, *args):
  57. func(self, *args)
  58. now = datetime.now().strftime("%X")
  59. name = "log_{}.txt".format('')
  60. log = "[{}] 当前执行步骤为{}:{} {}\n".format(now, step, func.__name__, args)
  61. sys.stdout.write("[{}] 当前执行步骤为:{} {}\n".format(now, func.__name__, args))
  62. with open(name, "a+") as f:
  63. f.write(log)
  64.  
  65. return wrapper
  66.  
  67. # 自动化所有用到的方法
  68. class AutoTest:
  69. """
  70. 自动化测试的主要脚本
  71. 所有行为在此类中编写
  72. 具体的操作及操作值,由配置表控制
  73. """
  74.  
  75. def __init__(self, driver):
  76. self.driver = driver
  77. self.action = ActionChains(driver)
  78.  
  79. @write_finish_info
  80. def get_url(self, entry):
  81. """
  82. 打开网页的方法
  83. 传入必须为列表list,会检测list长度是否2
  84. 如若不为list或长度不对,则中断后续操作
  85. 若不想中断,则把raise ValueError("get_url gets a wrong value")改为return即可
  86. """
  87. if not isinstance(entry, list) and len(entry) != 2:
  88. raise ValueError("get_url gets a wrong value")
  89. url, s = tuple(entry)
  90. self.driver.get(url)
  91. sleep(s)
  92.  
  93. @write_finish_info
  94. def maximize_window(self, s):
  95. sleep(s)
  96. self.driver.maximize_window()
  97.  
  98. @write_finish_info
  99. def write_in_xpath(self, entry):
  100. """
  101. 在xpath写入值的方法
  102. 传入必须为list,会检测list长度是否为3
  103. """
  104. if not isinstance(entry, list) and len(entry) != 3:
  105. raise ValueError("write_in_xpath gets a wrong value")
  106. xpath, words, s = tuple(entry)
  107. try:
  108. self.driver.find_element_by_xpath(xpath).send_keys(words)
  109. except Exception as e:
  110. print(e.error)
  111.  
  112. sleep(s)
  113.  
  114. @write_finish_info
  115. def write_in_id(self, entry):
  116. """
  117. 在id写入值的方法
  118. 传入必须为list,会检测list长度是否为3
  119. """
  120. if not isinstance(entry, list) and len(entry) != 3:
  121. raise ValueError("write_in_id gets a wrong value")
  122. id, words, s = tuple(entry)
  123. self.driver.find_element_by_id(id).send_keys(words)
  124. sleep(s)
  125.  
  126. @write_finish_info
  127. def click_xpath(self, entry):
  128. """
  129. 点击xpath的方法
  130. 传入必须为list,会检测list长度是否为2
  131. """
  132. if not isinstance(entry, list) and len(entry) != 2:
  133. raise ValueError("click_xpath gets a worng value")
  134. xpath, s = tuple(entry)
  135. self.driver.find_element_by_xpath(xpath).click()
  136. sleep(s)
  137.  
  138. @write_finish_info
  139. def clear_xpath(self, entry):
  140. """
  141. 点击xpath的方法
  142. 传入必须为list,会检测list长度是否为2
  143. """
  144. if not isinstance(entry, list) and len(entry) != 2:
  145. raise ValueError("click_xpath gets a worng value")
  146. xpath, s = tuple(entry)
  147. self.driver.find_element_by_xpath(xpath).clear()
  148. sleep(s)
  149.  
  150. @write_finish_info
  151. def refresh(self, s):
  152. """
  153. 在等待s秒后,refresh
  154. """
  155. sleep(s)
  156. self.driver.refresh()
  157.  
  158. @write_finish_info
  159. def drag_and_drop(self, entry):
  160. if not isinstance(entry, list) and len(entry) != 4:
  161. raise ValueError("drag_and_drop gets a wrong value")
  162. action = ActionChains(self.driver)
  163. source_xpath, target_xpath, s = tuple(entry)
  164. source = self.driver.find_element_by_xpath(source_xpath)
  165. target = self.driver.find_element_by_xpath(target_xpath)
  166. move_mouse_to_element(self.driver, target)
  167. action.drag_and_drop(source, target).perform()
  168. sleep(s)
  169.  
  170. @write_finish_info
  171. def drag_and_drop1(self, entry):
  172. if not isinstance(entry, list) and len(entry) != 3:
  173. raise ValueError("drag_and_drop gets a wrong value")
  174. action = ActionChains(self.driver)
  175. source_xpath, target_xpath, s = tuple(entry)
  176. source = self.driver.find_element_by_xpath(source_xpath)
  177. target = self.driver.find_element_by_xpath(target_xpath)
  178. move_mouse_to_element1(self.driver, target)
  179. action.drag_and_drop(source, target).perform()
  180. sleep(s)
  1. ----------------------------------------------------------------------------------------------
  1. from selenium import webdriver
  2. from time import sleep
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from selenium import webdriver
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver import ActionChains
  7. from selenium.common.exceptions import UnexpectedAlertPresentException
  8. from selenium.webdriver.common.action_chains import ActionChains
  9. from Login import *
  10. import time
  11.  
  12. driver = webdriver.Chrome(executable_path="/Users/jerry/PycharmProjects/untitled/venv/Goole/chromedriver")
  13.  
  14. auto = AutoTest(driver)
  15.  
  16. while True:
  17. step = 0
  18. login(auto, step)

有啥疑问欢迎提出,共同进步

by-咯咯

二次开发的Selenium Demo版本的更多相关文章

  1. efront二次开发记要

    efront系统是一套开源的在线学习系统,是用PHP编写的,内含“考试”功能.该系统的开源的是社区版,虽然看上去功能强大,但使用起来却很不符合国情.为了让公司使用,先做了一次最简化的二次开发,由于是最 ...

  2. 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单、书签等相关问题

    2015-03-02 更新文章,由于需求修改,更改了flexpaper插件,故增加第9.10.11小节,下载代码时请注意. 先废话几句.最近用到文档在线浏览功能,之前用的是print2flash(一个 ...

  3. 浩顺AC671指纹考勤机二次开发(demo)

    关于考勤机 AC671,是新换的机器,以前的那部机器,通过网络死活连接不上,换了AC671网络连接是好用了.但是,我要吐槽 浩顺的考勤机应该是卖了很多了吧,可是自带的软件太不给力,最后分析出来的数据一 ...

  4. phpcms v9版本二次开发四步曲

    今晚看了一下PHPCMS V9版本,做一个实例抛砖引玉,其实很简单,以下是二次开发的一个实例以旅游模块为例1.   在phpcms\modules目录下建立一个文件夹tour2.  在phpcms\m ...

  5. 基于redis实现tomcat8及以上版本的tomcat集群的session持久化实现(tomcat-redis-session-manager二次开发)

    前言: 本项目是基于jcoleman的tomcat-redis-session-manager二次开发版本 1.修改了小部分实现逻辑 2.去除对juni.jar包的依赖 3.去除无效代码和老版本tom ...

  6. Skyline 7 版本TerraExplorer Pro二次开发快速入门

    年底了,给大家整理了一下Skyline 7版本的二次开发学习初级入门教程,献给那些喜欢学习的年轻朋友. 我这整理的是Web控件版本的开发示例,里面页面代码保存成html,都可以直接运行的. 测试使用的 ...

  7. 浩顺考勤机二次开发(第二版,附实测可用的demo)

    1.背景 之前写过一次浩顺考勤机的二次开发,不过那个版本还是有一些问题,后来更换了新的考勤机,又拿到了新的二次开发包,所以就有了这次这个版本 2.关于考勤机的一些说明 2.1 首先要给考勤机设定ip, ...

  8. 深入理解基于selenium的二次开发

    对于做web端自动化测试的人来说,可能接触selenium比QTP还要多,但是我们在做基于selenium的二次开发的时候,经常会说到二次开发是为了易于维护,很多人可能不懂得维护的价值是什么,和到底要 ...

  9. openfire spark 二次 开发 服务插件

    ====================  废话 begin   ============================ 最近老大让我为研发平台增加即时通讯功能.告诉我用comet 在web端实现即 ...

随机推荐

  1. 【Docker】 windows10 docker 使用

    原文地址:https://www.aliyun.com/jiaocheng/872144.html 摘要:docker之前在Windows和MacOS上的版本都是通过Linux虚拟机的形式,而现在Do ...

  2. 分布式RPC系统框架Dubbo

    导读 Apache Dubbo是一款高性能.轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现. dubbo官网:点我直达 第一 ...

  3. 自用代码css获取任意网址的/favicon.ico的方法教程

    尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用这种办法获取 如果有好的方法望评论告知 谢谢 <img :ng-src="'http://'+list.url+'/fav ...

  4. Day9-Python3基础-多线程、多进程

    1.进程.与线程区别 2.python GIL全局解释器锁 3.线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者消费者模型 Que ...

  5. Spring事务梳理

    Spring事务传播行为 概述 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为 事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数 ...

  6. ios--->ios消息机制(NSNotification 和 NSNotificationCenter)

    问题的背景 IOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改 ...

  7. 机器学习李航——Adaboost课本例题实现

    例8.1Adaboost的例子 注意求D3或者D4的时候只需要把w替换一下就行,记得还得改阈值.这个代码算个半自动的,因为还需要手动改一下. import numpy as np def getA(e ...

  8. ROC 曲线

    Receiver Operating Characteristic (接收机操作特性曲线) 是以虚警率为横轴,以击中率为纵轴,长成如下模样: 所谓击中率(hit)是指将正样本判断为正样本的比例,而虚警 ...

  9. Kafka动态配置实现原理解析

    问题导读 Apache Kafka在全球各个领域各大公司获得广泛使用,得益于它强大的功能和不断完善的生态.其中Kafka动态配置是一个比较高频好用的功能,下面我们就来一探究竟. 动态配置是如何设计的? ...

  10. WPF另类实现摄像头录像

    WPF中使用第三方控件来直接进行录像的控件没有找到(aforgenet好像不维护了?WPFMediaKit好像只能实现摄像头拍照.收费的控件没有使用,不做评论.) 通过百度(感谢:https://ww ...