From 82f44e589fb3bc0986e956cde490fee8c844c5ee Mon Sep 17 00:00:00 2001 From: PAN Lei Date: Thu, 19 May 2022 14:28:40 +0200 Subject: [PATCH] fill the field of refresh --- utils/excel_reader.py | 6 +++++- workers/commandor_page.py | 43 ++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/utils/excel_reader.py b/utils/excel_reader.py index ca72027..df90efc 100644 --- a/utils/excel_reader.py +++ b/utils/excel_reader.py @@ -24,7 +24,11 @@ class ExcelHelper: raw_name = contact_dict['name'].strip() name = raw_name.split(' ') last_name = name[0] - first_name = name[-1] + if len(name) == 2: + first_name = name[-1] + else: + first_name = ''.join(name[1:len(name)]) + contact = ContactPojo(phone_number=contact_dict['phone'], last_name=last_name, first_name=first_name, diff --git a/workers/commandor_page.py b/workers/commandor_page.py index e01e122..869c733 100644 --- a/workers/commandor_page.py +++ b/workers/commandor_page.py @@ -29,7 +29,7 @@ OTP_TIMEOUT = 240 def get_random_wait_time() -> float: - wait_time = random.randint(0, 10) / 10.0 * 5 + wait_time = random.randint(0, 10) / 10.0 * 1 return wait_time @@ -41,7 +41,7 @@ class Tls(threading.local): class CommandorPage: tls = Tls() - def __init__(self, contact: ContactPojo, store_type=0, proxy_type = 0): + def __init__(self, contact: ContactPojo, store_type=0, proxy_type=0): self.otp_value = None self.logger = logging.getLogger("CommandorPage") self.is_finished = False @@ -86,13 +86,7 @@ class CommandorPage: "username": proxy_username, "password": params.PROXY_PASSWORD } - self._setName(self.contact.last_name, self.contact.first_name) - self._setPhoneCountryAndStore() - self._setPhoneNumber(self.contact.phone) - self._set_email(self.contact.mail) - self.setIdNumber(self.contact.passport) - # - self._checkCgu() + self.fill_fields() # wait for sms_code field # self.clickOnValidBtn() self.thread_event = e @@ -117,6 +111,15 @@ class CommandorPage: self.logger.info("timeout") self.termine() + def fill_fields(self): + self._set_name(self.contact.last_name, self.contact.first_name) + self._setPhoneCountryAndStore() + self._setPhoneNumber(self.contact.phone) + self._set_email(self.contact.mail) + self.setIdNumber(self.contact.passport) + # + self._checkCgu() + def start_browser(self, proxy, pwright, device) -> Union[str, None]: try: self.browser = pwright.webkit.launch(headless=False, timeout=90000, proxy=proxy) @@ -151,6 +154,8 @@ class CommandorPage: self.logger.info("page loaded") # self.logger.info("content is " + self.page.content()) self.logger.info("url is " + self.page.url) + if self.page.url == RDV_URL: + self.fill_fields() message = self.page.content() if CONFIRMED_MESSAGE in message: # publish the successful message @@ -183,14 +188,18 @@ class CommandorPage: except Exception as error: self.logger.error(error) - def _setName(self, lastName, firstName): + def _set_name(self, lastName, firstName): time.sleep(get_random_wait_time()) try: - self.page.evaluate("""(name)=> { - document.getElementById("surname").focus(); - document.getElementById("surname").value = name.lastName; + self.page.evaluate("""(name)=> { + let surname = document.getElementById("surname"); + if(surname.value.length == 0){ + surname.focus(); + surname.value = name.lastName; document.getElementById("name").focus(); - document.getElementById("name").value = name.firstName}""", {'lastName': lastName, 'firstName': firstName}) + document.getElementById("name").value = name.firstName + }} + """, {'lastName': lastName, 'firstName': firstName}) except Exception as error: self.logger.error(error) @@ -224,8 +233,10 @@ class CommandorPage: time.sleep(get_random_wait_time()) try: self.page.evaluate("""(email)=>{ - document.getElementById("email").focus(); - document.getElementById("email").value = email;}""", email) + let emailElement = document.getElementById("email") + if(emailElement.value.length == 0){ + emailElement.focus(); + document.getElementById("email").value = email;}}""", email) except Exception as error: self.logger.error(error)