2014-10-20 71 views
-1

我有一个案例,当我需要找出现在显示的按钮。我以这种方式尝试第一个 {请不要介意我的帮手功能。我认为,你可以很容易明白,Ui.find_el它几乎是一样的driver.find_element_by ....}:如何检查元素是否在屏幕上

if Ui.find_el(link.AuthorPopupNodes.LOGIN_EMAIL).is_displayed(): 
    pass 
else: 
    if Ui.find_el(link.HeaderNodes.LOGOUT_BUTTON).is_displayed(): 
     self.log_out() 
    Ui.wait_for_element(link.HeaderNodes.LOGIN_BUTTON, "Timeout: Wait for Login button!") 
    Ui.click_el(link.HeaderNodes.LOGIN_BUTTON) 

但随后开始下降的错误,驱动程序无法找到的元素,所以我盖它通过尝试,但它不适合我。

try: 
    if Ui.find_el(link.AuthorPopupNodes.LOGIN_EMAIL).is_displayed(): 
     pass 
    else: 
     if Ui.find_el(link.HeaderNodes.LOGOUT_BUTTON).is_displayed(): 
      self.log_out() 
     Ui.wait_for_element(link.HeaderNodes.LOGIN_BUTTON, "Timeout: Wait for Login button!") 
     Ui.click_el(link.HeaderNodes.LOGIN_BUTTON) 
except: 
    pass 

例如,如果第一个IF失败,它会从try中跳出来,我需要检查是否有LOGOUT按钮。是否有可能检查元素没有尝试,但也没有硒错误?

回答

1

你能赶上specific selenium errors,例如:

from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException 

try: 
    Ui.find_el(link.AuthorPopupNodes.LOGIN_EMAIL) 
except (NoSuchElementException, ElementNotVisibleException): 
    try: 
     Ui.find_el(link.HeaderNodes.LOGOUT_BUTTON) 
     self.log_out() 
    except (NoSuchElementException, ElementNotVisibleException): 
     pass 

    Ui.wait_for_element(link.HeaderNodes.LOGIN_BUTTON, "Timeout: Wait for Login button!") 
    Ui.click_el(link.HeaderNodes.LOGIN_BUTTON) 
+0

谢谢!现在我明白我需要像IF一样使用try。 – Michael 2014-10-22 20:47:22