Commit 22b54f82 authored by Lucas Ficheux's avatar Lucas Ficheux Committed by Vincent A

[sogecartenet] Improved stability of iter_history

Sometimes the search for transactions opens a popup
(even after limiting the search to one year) so now we close
it, reduce the length of the search by one month and retry.

Also force the return to the transaction search page after
selecting a card because the website sometimes does not take
us there automatically.
parent 34583835
......@@ -35,7 +35,7 @@ from weboob.browser.selenium import (
from .ent_pages import (
LoginPage, AccueilPage, AccountsPage, HistoryPage,
AccountsXlsPage, HistoryXlsPage,
AccountsXlsPage, HistoryXlsPage, DeferredQuery,
......@@ -159,7 +159,28 @@ class SogecarteEntrepriseBrowser(SeleniumBrowser):
self.selected_account =
index_last_date =
successful_download = False
is_retry = False
while index_last_date > 0:
successful_download =, retry=is_retry)
except DeferredQuery:
# This is raised when the site displays a popup to ask if we want to confirm a
# deferred query, deferred queries can be avoided by asking for less history
# here we simply retry with the xpath index of the date decreased by 1 essentialy
# decreasing the length of history requested by one month
self.logger.warning('Defered Query detected, retrying')
index_last_date -= 1
is_retry = True
self.logger.warning('Could not get transactions without deferred query for account %s' % self.selected_account)
if successful_download:
file_path = self.retry_find_file_path('requete_cumul', '.xls')
assert file_path, 'Could not find the downloaded file'
......@@ -40,6 +40,7 @@ from weboob.browser.elements import ItemElement, DictElement, method
from import retry
from weboob.browser.selenium import (
SeleniumPage, VisibleXPath, AnyCondition, AllCondition,
......@@ -148,6 +149,10 @@ class AccountsXlsPage(LoggedPage, XLSPage):
obj__service_number = CleanText(Dict('Numéro de prestation'))
class DeferredQuery(Exception):
class HistoryPage(LoggedPage, SeleniumPage):
is_here = VisibleXPath('//div[contains(text(), "Recherche opérations")]')
......@@ -157,7 +162,7 @@ class HistoryPage(LoggedPage, SeleniumPage):
self.browser.wait_xpath_clickable('//div[contains(@class, "v-widget")][div[div[@id="BTN_SEARCH"]]]')
def download_transactions(self):
def get_limit_date_index(self):
# We suppose we already selected an account.
el = self.driver.find_element_by_xpath('//input[contains(@placeholder, "De la date d\'arrêté")]')
......@@ -170,14 +175,34 @@ class HistoryPage(LoggedPage, SeleniumPage):
last_date_index = 0
for date in dates_list:
displayed_date = Date(CleanText('.'))(date)
delta = relativedelta(today, displayed_date)
if (
relativedelta(today, displayed_date).years >= 1 and
relativedelta(today, displayed_date).months > 0
delta.years >= 1 and
(delta.months > 0 or delta.days > 0)
last_date_index += 1
return last_date_index
def is_deferred_query(self):
# If we ask for too much history sometimes the site tells
# us that the query will be answered at a later time and asks to
# confirm, we want to avoid this behaviour
return 'Votre requête sera traitée en différé' in CleanText('//div[@role="dialog"]/div[@class="popupContent"]')(self.doc)
def download_transactions(self, last_date_index, retry=False):
# Select chosen date
if retry:
el = self.driver.find_element_by_xpath('//input[contains(@placeholder, "De la date d\'arrêté")]')
self.browser.wait_xpath_invisible('//p[contains(@class, "Notification-description")][contains(text(), "a bien été sélectionnée")]')
el = self.driver.find_element_by_xpath(
'//div[@id="VAADIN_COMBOBOX_OPTIONLIST"]//div[contains(@class, "suggestmenu")]//tr[%d]/td' % last_date_index
......@@ -187,6 +212,13 @@ class HistoryPage(LoggedPage, SeleniumPage):
# Submit search for this date
if self.is_deferred_query():
# Clicking no on the popup
el = self.driver.find_element_by_xpath('//div[@role="dialog"]/div[@class="popupContent"]//div[contains(@class, "button-friendly") and .//span[text()="Non"]]',)
raise DeferredQuery()
# Get data
......@@ -233,13 +265,16 @@ class HistoryPage(LoggedPage, SeleniumPage):
el = self.driver.find_element_by_xpath('//div[span[span[text()="Rechercher"]]]')
# Get the button of the right card (there might be multiple
# card with the same service number) and click it
el = self.driver.find_element_by_xpath('//tbody/tr/td[1][following-sibling::td[contains(text(), "%s")]]' % account._card_number)
el = self.driver.find_element_by_xpath(
'//tbody/tr/td[1][following-sibling::td[contains(text(), "%s")]]//div[contains(@class, "btnGrid-action")]' % account._card_number
self.browser.wait_xpath_visible('//p[contains(@class, "Notification-description")][contains(text(), "a bien été sélectionnée")]')
class HistoryXlsPage(LoggedPage, XLSPage):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment