python+selenium根据座标实现自动化测试

构思:登录成功后,获取页面座标后,模拟鼠标操作,实现点击事件

创建类,初始化信息
from selenium import webdriver
import time
import math,random
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class auto_test():
    def __init__(self):
        chrome_option = webdriver.ChromeOptions()
        chrome_option.add_argument('--start-maximized')
        chrome_option.add_argument('--disable-infobars')
        self.driver = webdriver.Chrome(chrome_options=chrome_option)
        #获取初始化值
        self.driver.get(url)
        self.driver.maximize_window()
        # 获取窗口大小
        data = self.driver.get_window_size()
        #存储运行失败的座标
        self.data_F=[]
        self.win_h = data['height']
        self.win_w = data['width']
获取随机座标,注意可是区域外的代码
 x=random.randint(30,self.win_w)
        y=random.randint(120,self.win_h)
        try:
            time.sleep(1)
            #根据座标偏移量进行
            ActionChains(self.driver).move_by_offset(x,y).click().perform()
            print("success")
            #恢复座标位置
            ActionChains(self.driver).move_by_offset(-x, -y).perform()
        except :
            self.data_F.append({x,y})
            print("Failed")
调试
		name=auto_test()
		name.auto_login()
		i=0
		for i in range(10000):
		    name.action_by_offset()
		    i+=1
		print(name.data_F,len(name.data_F))
出现的问题

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
原因:move_by_offset方法理解错了,默认为偏移量x,y,实际移动的位置是当前座标+x/y,故整体超出可视区域范围
处理方法:操作后,恢复座标位置为初始点

缺陷

  1. 点击事件触发的标签是否可点击不清楚,不明确
  2. 无法进行下拉框选项,导致有些数据无法获取到
  3. 不能进行输入框测试
  • BeautifulSoup解析dom获取可点击标签,返回class或者id或标签信息
  • 通过递归的方式遍历树
  • 针对下拉框、输入框进行方法编写
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章