封装ObjectMap.py工具类
五、 元素是否显示
def element_is_dispaly(self,driver,locate_type,locator_expression):
"""
元素是否显示
:param driver:
:param locate_type:
:param locator_expression:
:return:
"""
try:
driver.find_element(by=locate_type,value=locator_expression)
return True
except NoSuchElementException: #发生NoSuchElementException错误,说明页面中没有找到元素
return False
六、元素填值
def element_fill_value(self,driver,locate_type,locator_expression,fill_value,timeout=30):
"""
元素填值
:param driver:
:param locate_type:
:param locator_expression:
:param timeout:
:return:
"""
element=self.element_appear(
driver=driver,
locate_type=locate_type,
locator_expression=locator_expression,
timeout=timeout) #页面元素出现
try:
element.clear() #对值进输入框行清除
except StaleElementReferenceException: #页面元素未刷新出来,就对元素进行捕获,因此引发该异常
self.wait_for_read_state_complete(driver=driver) #等待页面完全加载
time.sleep(0.06)
element = self.element_appear(driver=driver,
locate_type=locate_type,
locator_expression=locator_expression,
timeout=timeout)#再进行一次等待元素的出现
try:
element.clear()
except Exception:
pass
except Exception:
pass
#对输入的字符进行处理
if type(fill_value) is int or type(fill_value) is float:
fill_value=str(fill_value)
try:
if not fill_value.endswith("\n"):
element.send_keys(fill_value)
self.wait_for_read_state_complete(driver=driver)
else:
fill_value=fill_value[:-1]
element.send_keys(fill_value)
element.send_keys(Keys.RETURN)
self.wait_for_read_state_complete(driver=driver)
except StaleElementReferenceException:
self.wait_for_read_state_complete(driver=driver)
time.sleep(0.06)
element = self.element_appear(driver=driver,
locate_type=locate_type,
locator_expression=locator_expression,
timeout=timeout)
element.clear()
if not fill_value.endswith("\n"):
element.send_keys(fill_value)
self.wait_for_read_state_complete(driver=driver)
else:
fill_value=fill_value[:-1]
element.send_keys(fill_value)
element.send_keys(Keys.RETURN)#回车键
self.wait_for_read_state_complete(driver=driver)
except Exception:
raise Exception("元素填值失败")
return True