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或標籤信息
  • 通過遞歸的方式遍歷樹
  • 針對下拉框、輸入框進行方法編寫
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章