airTest的第三方類庫中有圖像實別功能,根據官網的介紹,這個功能是能夠在Windows上用來定位元素,進行操作的。嘗試過以下腳本,發現真的可以。
?
from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium.webdriver.common.keys import Keys import time from selenium import * import os from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait import win32gui from airtest.core.api import * from airtest.cli.parser import cli_setup# 打開chrome 瀏覽器,可以用別的方式打開,我這里用webdriver。 driver = webdriver.Chrome(executable_path="C:\\Users\\xx.xxxx\\Documents\\AutoTesting\\xxxxxxxx\\chromedriver.exe")# 返回窗口標題為data:, - Google Chrome的句柄.這里是提前寫好的,因為知道每次打開chrome的時候都會顯示這個窗口標題。 hld = win32gui.FindWindow(None, "data:, - Google Chrome") shld = str(hld)# 這是airtest 連接chrome窗口的關鍵步驟。將剛剛取到的句柄傳入,連接airtest if not cli_setup():auto_setup(__file__, logdir=True, devices=["Windows:///" + shld])# 下面就可以實行點擊等操作了。在chrome窗口中,尋找預先截好的圖--"tpl1559122892842.png" ,這張圖要放在工程內。一旦尋找到,就會點擊,也就是touch操作。 touch(Template(r"tpl1559122892842.png", record_pos=(0.16, -0.011), resolution=(1391, 746))) touch(Template(r"tpl1559122886202.png", record_pos=(0.244, -0.092), resolution=(1391, 746)))# 直接text操作,在目前光標所在的位置執行。所以執行這一步之前,要在上一步先以touch的方式,將光標移在目標輸入框內。 text("www.baidu.com") touch(Template(r"tpl1559123207157.png", record_pos=(0.315, -0.116), resolution=(1391, 746)))
?