From 5b4cdf135148622690798444c4fc4e15dd23ca86 Mon Sep 17 00:00:00 2001 From: Quentin Defenouillere Date: Thu, 4 Jul 2019 18:21:08 +0200 Subject: [PATCH] [weboob/browser/elements] Implement the empty_xpath attribute The empty_xpath attribute is used to fetch an explicit message on a page when there is no account, no transaction or no investment for example. The goal of this attribute is to avoid silent bugs when an item page has completely changed and the item_xpath does not match any element anymore: in these cases we don't scrape any element but we don't crash either ; whereas if empty_xpath is defined at least we will have a warning that the page may have changed. --- weboob/browser/elements.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/weboob/browser/elements.py b/weboob/browser/elements.py index 8f01f78b34..3e16b31902 100644 --- a/weboob/browser/elements.py +++ b/weboob/browser/elements.py @@ -155,6 +155,7 @@ def handle_loaders(self): class ListElement(AbstractElement): item_xpath = None + empty_xpath = None flush_at_end = False ignore_duplicate = False @@ -176,8 +177,13 @@ def find_elements(self): sufficient. """ if self.item_xpath is not None: - for el in self.el.xpath(self.item_xpath): - yield el + element_list = self.el.xpath(self.item_xpath) + if element_list: + for el in element_list: + yield el + elif self.empty_xpath is not None and not self.el.xpath(self.empty_xpath): + # Send a warning if no item_xpath node was found and an empty_xpath is defined + self.logger.warning('No element matched the item_xpath and the defined empty_xpath was not found!') else: yield self.el -- GitLab