Saldo Cartão Refeição Santander - AppDaemon + Python + Selenium

Estive a seguir os teus passos mas tive alguns problemas, relacionados com atualizações do Selenium e com o webdriver.Chrome
Deixo o meu código do GetSantanderBalance.py que alterei e no meu caso ficou a funcionar:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import appdaemon.plugins.hass.hassapi as hass
import time
import datetime

class GetSantanderBalance(hass.Hass):

  def initialize(self):
    self.log("Starting Santander App")
    runtime  = datetime.time(0 ,0 ,0)
    self.run_minutely(self.CallListenProcess,runtime) 

  def CallListenProcess(self, kwargs):
    self.log("--------- SANTANDER BALANCE ---------")
    # listen for state ON for update balance
    self.listen_state(self.StartProcess,"switch.updatebalance", new = "on", timeout = 59)

  #Callback Function to Start the process 
  def StartProcess(self, entity, attribute, old, new, kwargs):
    self.log("-- SWITCH ON --")
    balance = self.GetBalance()
    self.log("Balance: " + balance)
    try:
      self.set_state('sensor.santander_balance', state = balance)
    except Exception as e:
      self.log("Set Sensor State Error: " + str(e))
    try:
      self.set_state("switch.updatebalance", state = old)
    except Exception as e:
      self.log("Set Swith State Error: " + str(e))

  def GetBalance(self):
    timeout = 4
    balance = "00,00"
    try:
      self.log("Start GetBalance")
      chrome_options = webdriver.ChromeOptions()
      chrome_options.add_argument('--headless')
      chrome_options.add_argument('--no-sandbox')
      chrome_options.add_argument('--disable-gpu')
      chrome_options.add_argument('--disable-dev-shm-usage')
      browser = webdriver.Chrome(options=chrome_options)
      try:
        self.log("Start Request")
        browser.get('https://www.particulares.santander.pt/pagina/indice/0,,840_1_2,00.html')
        time.sleep(timeout)
        try:
          browser.find_element("id",'onetrust-accept-btn-handler').click()
        except Exception as e:
          self.log("No cookies")

        self.log("Login form")
        browser.switch_to.frame('ws')
        browser.find_element(By.XPATH,'//input[@autocomplete="userNameInputId"]').send_keys("************** CARD NUMBER ***********")
        browser.find_element(By.XPATH,'//input[@autocomplete="passwordInputId"]').send_keys("************** CARD CODE ***********")
        browser.find_element(By.ID,'submit_button').click()
        time.sleep(timeout)

        self.log("Success Login")
        browser.switch_to.frame('ws')
        time.sleep(timeout)

        self.log("Extracted Balance")
        balance = str(browser.find_element(By.XPATH,'//p[@class="balance-value text-green"]').text)
        browser.switch_to.default_content()

        self.log("Start Logout")
        browser.find_element(By.ID,'quick-logout').click()
        browser.close()
        self.log("Closed")
      except Exception as e:
        self.log("Request Error: " + str(e))
        browser.close()
    except Exception as e:
      self.log("GetBalance Error: " + str(e))
    return balance

Copyright © 2017-2021. Todos os direitos reservados
CPHA.pt - info@cpha.pt


FAQ | Termos de Serviço/Regras | Política de Privacidade