def splid_data(self,year,month,day):
ele = self.driver.find_element(By.XPATH,"//*[contains(@text,‘确认’)]")
if ele!=None:
#元素的宽
width = ele.size[‘width’]
# 元素的高
height = ele.size[‘height’]
# 控件相对于整个屏幕的高度
y = ele.location[‘y’]
self.switchWebContext()
action = TouchAction(self.driver)
if year !=0:
defaultYear = self.driver.find_element(By.XPATH, "//*[@class='van-ellipsis' and contains(text(),'年')]")
diffYear = int(defaultYear.get_attribute("innerText").replace(" 年", "")) - year
diffYear = abs(diffYear)
for item in range(1,abs(diffYear)+1):
action.press(x=width / 6,y = y + height * 13 / 20).wait(500).move_to(x=width / 6,y=y + height * 62 / 100).release().perform()
sleep(2)
if int(month) !=0:
defaultMonth = self.driver.find_element(By.XPATH, "//*[@class='van-ellipsis' and contains(text(),'月')]")
diffMonth = int(defaultMonth.get_attribute("innerText").replace(" 月", "")) - int(month)
for item in range(1, abs(diffMonth) + 1):
action.press(x=width * 3 / 6, y = y + height * 13 / 20).wait(500).move_to(x=width * 3 / 6, y=y + height * 62 / 100).release().perform()
sleep(2)
if int(day)!=0:
defaultDay = self.driver.find_element(By.XPATH, "//*[@class='van-ellipsis' and contains(text(),'日')]")
diffDay = int(defaultDay.get_attribute("innerText").replace(" 日", "")) - int(day)
for item in range(1, abs(diffDay) + 1):
action.press(x=width * 5 / 6, y = y + height * 13 / 20).wait(500).move_to(x=width * 5 / 6, y=y + height * 62 / 100).release().perform()
sleep(2)
self.driver.switch_to.context("NATIVE_APP")
self.SPDBBankButtonClick('确认',1)
@classmethod
def switchWebContext(self):
contexts = self.driver.contexts
print(contexts)
logger.info(f"当前页面的context:{contexts}")
for item in contexts:
if item.startswith('WEBVIEW'):
self.driver.switch_to.context(item)
print("切换webview成功")
now = self.driver.current_context
logger.info(f"切换后的context:{now}")
break
@classmethod
def SPDBBankButtonClick(self, text, textIndex):
try:
a = datetime.now()
xpath = f'//*[normalize-space(@text)="{text}"]';
xpath = xpath + "[last()]" if textIndex == -1 else xpath + "[" + str(textIndex) + "]"
self.wait(xpath)
self.click(By.XPATH, xpath)
b = datetime.now()
senconds = (b - a).seconds
print("按钮点击耗时" + str(senconds) + "秒")
print("点击按钮【" + text + "】成功")
except:
print("点击失败")
raise
@classmethod
def click(self, locator, value):
self.driver.find_element(locator, value).click();
@classmethod
def wait(self,loc):
try:
WebDriverWait(self.driver, 20, 0.5).until(EC.visibility_of_element_located((By.XPATH, loc)))
except Exception as e:
print(f"元素 不存在{loc}")
raise