简体   繁体   English

在 Appium Python 中尝试 if elif

[英]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.所以我必须检查下面提到的 3 个元素中的任何一个。 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元素是: 1. 退出按钮已经存在(因为用户已经登录) 2. 帐户名称

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.但是发生的情况是我的 appium 正在执行第一个 IF 循环并等待元素注销并以错误消息结束。

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: elif 不应该像这样无缩进:

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.如果您想在触发操作之前检查元素是否存在,只需创建一个方法,该方法根据元素的 isDisplayed 属性返回一个布尔值。

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.并在测试 sript 的每个操作之前调用 IsElementDisplayed。

Because there's no element that has the name "sign out", find_element_by_name() is throwing an exception that isn't being handled.因为没有名为“退出”的元素,所以find_element_by_name()抛出了一个未被处理的异常。 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)如另一个答案中所述,设置隐式等待会等待元素出现并重复搜索元素直到计时器到期(以下代码中的 20 秒)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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