简体   繁体   中英

Trying if ellif in Appium Python

I need to check if user has already logged into the application. So I have to check for any of the 3 elements below mentioned are present. If anyone of them is present, user is logged in and I need to click sign out button.

The elements are : 1. sign out button already present(since user is already signed in ) 2. Account name

My script is like :

 if(wd.find_element_by_name("sign out").is_displayed()):
        wd.find_element_by_name("sign out").click()      
 elif(wd.find_element_by_name("usr_name").is_displayed()):
        wd.find_element_by_name("usr_name").click()
        wd.find_element_by_name("menu_close").click()
        wait("sign out")
        wd.find_element_by_name("sign out").click()
 else:
        print"NOt Signed in"

But what happens is my appium is executing the first IF Loop and waiting for the element sign out and ends with an error message.

An element could not be located on the page using the given search parameters.

Where I am doing wrong ? Usually how can I check if an element is present then click it, like that. Please help me.

shouldn't the elif be unindented like this:

if(wd.find_element_by_name("sign out").is_displayed()):
        wd.find_element_by_name("sign out").click()      
elif(wd.find_element_by_name("usr_name").is_displayed()):
        wd.find_element_by_name("usr_name").click()
        wd.find_element_by_name("menu_close").click()
        wait("sign out")
        wd.find_element_by_name("sign out").click()
else:
        print"NOt Signed in"

您应该在每个命令之后使用wd.implicitly_wait(30) ,以便 appium 服务器等待下一个元素可见

If you want to check an element is present before firing an action just create a method which returns a boolean value based on the isDisplayed property for the element.

Something like this :

def IsElementDisplayed():
    try: 
       return wd.find_element_by_name("sign out").is_displayed()
    except:
       return false

And call the IsElementDisplayed before each action from the test sript.

Because there's no element that has the name "sign out", find_element_by_name() is throwing an exception that isn't being handled. One way to solve this is to write a wrapper for searching for elements that includes exception handling. As mentioned in another answer, setting the implicit wait bakes in waiting for elements to be present and repeatedly searches for the element until the timer expires (20s in the code below)

from selenium.common.exceptions import NoSuchElementException

wd.implicitly_wait(20)

sign_out = find_element("sign out")
usr_name = find_element("usr_name")

if sign_out is not None and sign_out.is_displayed():
    sign_out.click()
elif usr_name is not None and usr_name.is_displayed():
    usr_name.click()
    menu_close = find_element("menu_close")
    menu_close.click()
    sign_out = find_element("sign out")
    sign_out.click()
else:
    print("Not signed in")

def find_element(self, element_text):
    try:
        element = wd.find_element_by_name(element_text)
    except NoSuchElementException:
        return None
    return element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM