vendredi 20 octobre 2017

What is a good way of handling exception for NoSuchElementFound on a locators class (Page Object Model)?

In my main program:

lala = home_page.find_invitee_by_name("Lala")

In my page.py class:

class HomePage(Page):
    def find_invitee_by_name(self, name):
        element = self.driver.find_element(*HomePageLocators.INVITEE_BY_NAME(name))
        return Invitee(element)

In my locators.py:

class HomePageLocators():
    INVITE_FORM = (By.ID, "registrar")
    INVITE_FORM_NAME_FIELD = (By.CSS_SELECTOR, "#registrar input[name='name']")
    TOGGLE_RESPONDERS_CHECKBOX = (By.CSS_SELECTOR, ".main > div input[type='checkbox']")
    INVITEE_BY_NAME = lambda name: (By.XPATH, f"//span[text() = '{name}']/..")

I would like to know the best ways handling INVITEE_BY_NAME whenever an element is not found. Like returning a boolean false value rather than an exception (selenium.common.exceptions.NoSuchElementException).

Aucun commentaire:

Enregistrer un commentaire