Advertisement
bulfaitelo

Untitled

Jan 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.18 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # =========== IMPORTS ===========
  4. from datetime import datetime
  5. import dateutil.relativedelta
  6. from time import sleep
  7. import sys
  8. from selenium import webdriver
  9. from selenium.webdriver import FirefoxOptions
  10. from selenium.webdriver.common.keys import Keys
  11. from selenium.webdriver.common.by import By
  12. from selenium.webdriver.support import expected_conditions as EC
  13. from selenium.webdriver.support.ui import Select
  14. from selenium.webdriver.support.ui import WebDriverWait
  15. from selenium.common.exceptions import NoSuchElementException
  16. # ===============================
  17.  
  18.  
  19. # ========== FUNCTIONS ==========
  20. # Check if xpath existis
  21. def is_element_present_xpath(xpath):
  22.     try: firefox.find_element_by_xpath(xpath)
  23.     except NoSuchElementException: return False
  24.     return True
  25. # ===============================
  26.  
  27. print('[ {"inicio": "%s"},' % str(datetime.now()))
  28. # necessario para funcionar remotamente
  29. opts = FirefoxOptions()
  30. opts.add_argument("--headless")
  31. firefox = webdriver.Firefox(firefox_options=opts)
  32. # ============================================
  33.  
  34. # parametros
  35. user_login = sys.argv[1]
  36. user_pass = sys.argv[2]
  37. wait_time = 10
  38. # =====================================
  39.  
  40. # PAGINA DE LOGIN
  41. firefox.get('https://tesourodireto.bmfbovespa.com.br/portalinvestidor/')
  42. try
  43.     # preenchendo formulario de login
  44.     login = WebDriverWait(firefox, wait_time).until(EC.presence_of_element_located((By.ID, 'BodyContent_txtLogin')))
  45.     password = WebDriverWait(firefox, wait_time).until(EC.presence_of_element_located((By.ID, 'BodyContent_txtSenha')))
  46.     login.send_keys("", user_login)
  47.     password.send_keys("", user_pass)
  48.     login_attempt = WebDriverWait(firefox, wait_time).until(EC.presence_of_element_located((By.ID, 'BodyContent_btnLogar')))
  49.     login_attempt.click()
  50.     # ====================================
  51.  
  52.     # pagina de protocolos
  53.     firefox.get('https://tesourodireto.bmfbovespa.com.br/portalinvestidor/consulta-protocolo.aspx')
  54.  
  55.  
  56.     # INVESTIMENTOS
  57.     # selecionando a operação
  58.     select_operacao = Select(firefox.find_element_by_id('BodyContent_ddlOperacao'))
  59.     select_operacao.select_by_visible_text('Investimento')
  60.  
  61.     # selecionando a data
  62.     key_data_inicial = datetime.now()
  63.     key_data_final = key_data_inicial - dateutil.relativedelta.relativedelta(months=1)
  64.     # data inicial
  65.     data_inicial = firefox.find_element_by_id('BodyContent_dtRealizacaoInicial')
  66.     data_inicial.send_keys("", key_data_final.strftime("%d%m%Y"))
  67.     # data_inicial.send_keys("", key_data_final.strftime("17102017"))
  68.     # data final
  69.     data_final = firefox.find_element_by_id('BodyContent_dtRealizacaoFinal')
  70.     data_final.send_keys("", key_data_inicial.strftime("%d%m%Y"))
  71.     # data_final.send_keys("", key_data_inicial.strftime("13062018"))  
  72.     # clicando em consulta
  73.     btn_consultar = firefox.find_element_by_id('BodyContent_btnConsultar')
  74.     btn_consultar.click()
  75.  
  76.     # =====================================
  77.  
  78.     protocolos = firefox.find_elements_by_xpath("//table[contains(@class, 'responsive')]/tbody[2]/tr[contains(@class, 'nowrap')]")
  79.  
  80.     if is_element_present_xpath("//table[contains(@class, 'responsive')]/tbody[2]/tr[contains(@class, 'nowrap')][1]/td[2]"):    
  81.         for protocolo in protocolos:
  82.             numero_protocolo = protocolo.find_element_by_xpath('./td[1]').text
  83.             operacao = protocolo.find_element_by_xpath('./td[2]').text
  84.             situacao = protocolo.find_element_by_xpath('./td[3]').text
  85.             realizacao = datetime.strptime(protocolo.find_element_by_xpath('./td[4]').text, '%d/%m/%Y')
  86.             liquidacao = protocolo.find_element_by_xpath('./td[5]').text
  87.             if liquidacao:        
  88.                 liquidacao = datetime.strptime(liquidacao, '%d/%m/%Y')
  89.            
  90.             detalhes = protocolo.find_element_by_xpath('./td[6]/img')
  91.             detalhes.click()
  92.             sleep(2)
  93.             # modal detalhes
  94.             # modal_frame = firefox.switch_to.frame('modal')    
  95.             detalhes_modal = firefox.find_element_by_xpath('//*[@id="modal"]')    
  96.             # Dados Modal
  97.             nome_representante = detalhes_modal.find_element_by_xpath('./div[1]/div[2]/div[4]/div[2]/label').text.split(' - ')
  98.             nome_representante = nome_representante[0]
  99.             titulo = detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[1]').text
  100.             quantidade = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[2]').text).replace('.', '').replace(',','.')
  101.             valor_unitario = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[3]').text).replace('.', '').replace(',','.')
  102.             taxa_juros = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[4]').text).replace('.', '').replace(',','.')
  103.             taxa_b3 = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[5]').text).replace('.', '').replace(',','.')
  104.             taxa_custodia = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[6]').text).replace('.', '').replace(',','.')
  105.             valor_total = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[7]').text).replace('.', '').replace(',','.')
  106.             # print valores
  107.             print('{ "numero_protocolo": "%s", "operacao": "%s", "situacao": "%s", "realizacao": "%s", "liquidacao": "%s", "representante": "%s", "titulo": "%s", "quantidade": "%s", "valor_unitario": "%s", "taxa_juros": "%s", "taxa_b3": "%s", "taxa_custodia": "%s", "valor_total": "%s" }, ' % (numero_protocolo, operacao, situacao, realizacao, liquidacao, nome_representante, titulo, quantidade, valor_unitario, taxa_juros, taxa_b3, taxa_custodia, valor_total))#.encode('utf8')
  108.             # fechando modal
  109.             sair_modal = detalhes_modal.find_element_by_class_name('close-reveal-modal')
  110.             sair_modal.click()
  111.     # INVESTIMENTOS -- FIM
  112.  
  113.     # RESGATE
  114.     # Reiniciando a Consulta
  115.     btn_consultar = firefox.find_element_by_id('BodyContent_btnConsultar')
  116.     btn_consultar.click()
  117.     # selecionando a operação
  118.     select_operacao = Select(firefox.find_element_by_id('BodyContent_ddlOperacao'))
  119.     select_operacao.select_by_visible_text('Resgate')
  120.  
  121.     # selecionando a data
  122.     key_data_inicial = datetime.now()
  123.     key_data_final = key_data_inicial - dateutil.relativedelta.relativedelta(months=1)
  124.     # data inicial
  125.     data_inicial = firefox.find_element_by_id('BodyContent_dtRealizacaoInicial')
  126.     data_inicial.send_keys("", key_data_final.strftime("%d%m%Y"))
  127.     # data_inicial.send_keys("", key_data_final.strftime("17102017"))
  128.     # data final
  129.     data_final = firefox.find_element_by_id('BodyContent_dtRealizacaoFinal')
  130.     data_final.send_keys("", key_data_inicial.strftime("%d%m%Y"))
  131.     # data_final.send_keys("", key_data_inicial.strftime("13062018"))  
  132.  
  133.     # clicando em consulta
  134.     btn_consultar = firefox.find_element_by_id('BodyContent_btnConsultar')
  135.     btn_consultar.click()
  136.  
  137.     # =====================================
  138.  
  139.     protocolos = firefox.find_elements_by_xpath("//table[contains(@class, 'responsive')]/tbody[2]/tr[contains(@class, 'nowrap')]")
  140.     if is_element_present_xpath("//table[contains(@class, 'responsive')]/tbody[2]/tr[contains(@class, 'nowrap')][1]/td[2]"):    
  141.         for protocolo in protocolos:
  142.             numero_protocolo = protocolo.find_element_by_xpath('./td[1]').text
  143.             operacao = protocolo.find_element_by_xpath('./td[2]').text
  144.             situacao = protocolo.find_element_by_xpath('./td[3]').text
  145.             realizacao = datetime.strptime(protocolo.find_element_by_xpath('./td[4]').text, '%d/%m/%Y')
  146.             liquidacao = protocolo.find_element_by_xpath('./td[5]').text
  147.             if liquidacao:        
  148.                 liquidacao = datetime.strptime(liquidacao, '%d/%m/%Y')
  149.            
  150.             detalhes = protocolo.find_element_by_xpath('./td[6]/img')
  151.             detalhes.click()
  152.             sleep(2)
  153.             # modal detalhes
  154.             # modal_frame = firefox.switch_to.frame('modal')    
  155.             detalhes_modal = firefox.find_element_by_xpath('//*[@id="modal"]')    
  156.             # Dados Modal
  157.             nome_representante = detalhes_modal.find_element_by_xpath('./div[1]/div[2]/div[4]/div[2]/label').text.split(' - ')
  158.             nome_representante = nome_representante[0]
  159.             titulo = detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[1]').text
  160.             quantidade = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[2]').text).replace('.', '').replace(',','.')
  161.             valor_unitario = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[3]').text).replace('.', '').replace(',','.')
  162.             taxa_juros = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[4]').text).replace('.', '').replace(',','.')
  163.             taxa_b3 = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[5]').text).replace('.', '').replace(',','.')
  164.             taxa_custodia = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[6]').text).replace('.', '').replace(',','.')
  165.             valor_total = (detalhes_modal.find_element_by_xpath('./div[2]/div/div/table/tbody/tr/td[7]').text).replace('.', '').replace(',','.')
  166.             # print valores
  167.             print('{ "numero_protocolo": "%s", "operacao": "%s", "situacao": "%s", "realizacao": "%s", "liquidacao": "%s", "representante": "%s", "titulo": "%s", "quantidade": "%s", "valor_unitario": "%s", "taxa_juros": "%s", "taxa_b3": "%s", "taxa_custodia": "%s", "valor_total": "%s" }, ' % (numero_protocolo, operacao, situacao, realizacao, liquidacao, nome_representante, titulo, quantidade, valor_unitario, taxa_juros, taxa_b3, taxa_custodia, valor_total))#.encode('utf8')
  168.             # fechando modal
  169.             sair_modal = detalhes_modal.find_element_by_class_name('close-reveal-modal')
  170.             sair_modal.click()
  171.     # RESGATE -- FIM
  172.  
  173.     # Fechar navegador
  174.     firefox.quit()
  175.     print('{"fim": "%s"} ]' % str(datetime.now()))
  176. except Exception:  
  177.     logging.exception('DEU ERRO, OLHE erro.png')
  178.     firefox.save_screenshot('erro.png')
  179.     firefox.quit()
  180.     raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement