做自动化测试的时候,可能需要根据弹出的Toast提示来做下一步判断。这里记录一下获取Toast内容的方法,同时巩固一下显示等待的方法之一WebDriverWait。
from selenium.webdriver.support import expected_conditions as ec def find_toast(self, driver, message, timeout=10, poll=0.01):
try:
message = '//*[@text=\'{}\']'.format(message) #Toast内容
element = WebDriverWait(driver, timeout,poll).until(ec.presence_of_element_located((By.XPATH, message)))
return True
except Exception as e:
print(("Get Toast Error : ", e))
return False
presence_of_element_located可以替换为
- title_is
- title_contains
- presence_of_element_located
- visibility_of_element_located # 和 presence_of_element_located 的作用几乎一样
- visibility_of
- presence_of_all_elements_located
- text_to_be_present_in_element
- text_to_be_present_in_element_value
- frame_to_be_available_and_switch_to_it
- invisibility_of_element_located
- element_to_be_clickable - it is Displayed and Enabled.
- staleness_of
- element_to_be_selected
- element_located_to_be_selected
- element_selection_state_to_be
- element_located_selection_state_to_be
- alert_is_present
By.XPATH中的XPATH可替换为'CLASS_NAME', 'CSS_SELECTOR', 'ID', 'LINK_TEXT', 'NAME', 'PARTIAL_LINK_TEXT', 'TAG_NAME'