Skip to content

Instantly share code, notes, and snippets.

@luxu
Created September 9, 2018 20:24
Show Gist options
  • Save luxu/5fd422dad9e1b39c1c0f44c16f58b4dc to your computer and use it in GitHub Desktop.
Save luxu/5fd422dad9e1b39c1c0f44c16f58b4dc to your computer and use it in GitHub Desktop.
Site Rede - Cartões
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep
import io
# import os
import json
def write(data):
# BASE_DIR = os.path.dirname(__file__)
# with open(os.path.join(BASE_DIR, 'data.json'), 'w') as outfile:
with io.open('data.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(data,
indent=4, sort_keys=True,
separators=(',', ': '), ensure_ascii=False)
outfile.write(str_)
def main(driver):
url = 'http://www.userede.com.br/login/'
driver.get(url)
login = driver.find_element_by_name('usuario')
senha = driver.find_element_by_name('senha')
botao = driver.find_element_by_xpath('//*[@id="frmLogin"]/button')
login.send_keys('')
senha.send_keys('')
botao.send_keys(Keys.ENTER)
wait = WebDriverWait(driver, 15)
wait.until(EC.title_contains("Portal - Home"))
url = 'https://www.userede.com.br/sites/fechado/extrato/Paginas/pn_relatorios.aspx'
driver.get(url)
wait.until(EC.element_to_be_clickable((
By.ID, 'ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_ddlRelatorioSelectBoxItContainer')
))
driver.find_element_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_ddlRelatorioSelectBoxItArrowContainer"]'
).click()
driver.find_element_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_ddlRelatorioSelectBoxItOptions"]/li[1]/a/span'
).click()
sleep(1)
driver.find_element_by_xpath(
'// *[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_consultaPV_ddlTipoAssociacaoSelectBoxItArrowContainer"]'
).click()
driver.find_element_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_consultaPV_ddlTipoAssociacaoSelectBoxItOptions"]/li[1]/a/span'
).click()
sleep(1)
dt_inicio = driver.find_element_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_txtDataInicial"]'
)
dt_inicio.click()
dt_inicio.send_keys('03')
dt_inicio.send_keys('09')
dt_inicio.send_keys('2018')
dt_fim = driver.find_element_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_txtDataFinal"]'
)
dt_fim.send_keys("06")
dt_fim.send_keys("09")
dt_fim.send_keys("2018")
sleep(1)
driver.find_element_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_filtroControl_btnBuscar"]'
).click()
try:
total_no_período_valor_bruto = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_rcTotalizadores_qiValoresConsolidados"]/div[2]/div/div[1]/div[2]'
)[0]
except:
print('Não há movimento para o período informado!')
# write(u'Não há movimento para o período informado!'.upper())
return
total_no_período_valor_bruto = total_no_período_valor_bruto.text
total_no_periodo_valor_liquido = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_rcTotalizadores_qiValoresConsolidados"]/div[2]/div/div[2]/div[2]'
)[0]
total_no_periodo_valor_liquido = total_no_periodo_valor_liquido.text
valores_consolidados = {
'total_no_período_valor_bruto': total_no_período_valor_bruto,
'total_no_periodo_valor_liquido': total_no_periodo_valor_liquido
}
try:
mastercard = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_rcTotalizadores_pnlTotaisBandeira"]/div[2]/div[1]/div[2]/div[2]'
)[0]
mastercard = mastercard.text
except:
mastercard = 0
try:
visa = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_rcTotalizadores_pnlTotaisBandeira"]/div[2]/div[2]/div[2]/div[2]'
)[0]
visa = visa.text
except:
visa = 0
try:
hipercard = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_rcTotalizadores_pnlTotaisBandeira"]/div[2]/div[3]/div[2]/div[2]'
)[0]
hipercard = hipercard.text
except:
hipercard = 0
try:
elo = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_rcTotalizadores_pnlTotaisBandeira"]/div[2]/div[4]/div[2]/div[2]'
)[0]
elo = elo.text
except:
elo = 0
total_por_bandeira = {
'mastercard': mastercard,
'visa': visa,
'hipercad' : hipercard,
'elo': elo
}
cont = 1
tbody = '//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_dvRelatorio"]/table/tbody/tr'
for row in driver.find_elements_by_xpath(tbody):
print(cont)
cell = row.find_element_by_xpath(
u'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_dvRelatorio"]/table/tbody/tr[{}]/td[5]/a'
.format(cont)
)
print(cell.text)
cell.click()
pagina_final(driver)
element_present = EC.presence_of_element_located(
(By.XPATH,
'//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_dvRelatorio"]/table/tbody/tr[1]/td[5]/a'
)
)
WebDriverWait(driver, 5).until(element_present)
driver.navigate().refresh()
from pdb import set_trace
set_trace()
print("Voltou a PÁGINA PRINCIPAL")
cont+=1
relatórioVendas = {}
dados = 0
for row in driver.find_elements_by_xpath(tbody):
nro_estabelecimento = row.find_elements_by_tag_name("td")[0].text
data_d_venda = row.find_elements_by_tag_name("td")[1].text
data_de_recebimento = row.find_elements_by_tag_name("td")[2].text
prazo_de_recebimento = row.find_elements_by_tag_name("td")[3].text
resumo_vendas = row.find_elements_by_tag_name("td")[4].text
qt_vendas = row.find_elements_by_tag_name("td")[5].text
modalidade = row.find_elements_by_tag_name("td")[6].text
bandeira = row.find_elements_by_tag_name("td")[7].text
valor_bruto = row.find_elements_by_tag_name("td")[8].text
valor_descontado = row.find_elements_by_tag_name("td")[9].text
correcoes = row.find_elements_by_tag_name("td")[10].text
valor_liquido = row.find_elements_by_tag_name("td")[11].text
itens = {
'nº do estabelec.': nro_estabelecimento,
'data da venda': data_d_venda,
'data de receb.': data_de_recebimento,
'prazo de receb.': prazo_de_recebimento,
'resumo de vendas': resumo_vendas,
'qtde. de vendas': qt_vendas,
'modalidade': modalidade,
'bandeira': bandeira,
'valor bruto (R$)': valor_bruto,
'valor descontado (R$)': valor_descontado,
'correções (R$)': correcoes,
'valor líquido (R$)*': valor_liquido,
}
relatórioVendas[dados] = itens
dados += 1
tfoot = '//*[@id="ctl00_m_g_99009a3a_150b_4bb6_8338_75cdeaf73726_ctl00_RelatorioCredito_dvRelatorio"]/table/tfoot'
for row in driver.find_elements_by_xpath(tfoot):
total_coluna_valor_bruto = row.find_elements_by_tag_name("td")[1].text
total_coluna_valor_descontado = row.find_elements_by_tag_name("td")[2].text
total_coluna_correcoes = row.find_elements_by_tag_name("td")[3].text
total_coluna_valor_liquido = row.find_elements_by_tag_name("td")[4].text
total_no_periodo = {
'total coluna valor bruto': total_coluna_valor_bruto,
'total coluna valor descontado': total_coluna_valor_descontado,
'total coluna correcoes': total_coluna_correcoes,
'total coluna valor liquido': total_coluna_valor_liquido,
}
data = {
'relatório de vendas - crédito': relatórioVendas,
'total no periodo': total_no_periodo,
'valores consolidados': valores_consolidados,
'total_por_bandeira': total_por_bandeira,
}
# write(data)
# sleep(10)
# selenium.click("xpath=//a[contains(@href,'listDetails.do') and @id='oldcontent']")
def pagina_final(driver):
data_da_venda = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[1]/div/div/div[1]'
)[0]
data_da_venda = data_da_venda.text
data_do_processamento = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[1]/div/div/div[2]/div[2]'
)[0]
data_do_processamento = data_do_processamento.text
resumo_vendas = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[1]/div/div/div[3]/div[2]'
)[0]
resumo_vendas = resumo_vendas.text
bandeira = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[2]/div/div/div[1]/div[2]'
)[0]
bandeira = bandeira.text
qtde_de_vendas = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[2]/div/div/div[2]/div[2]'
)[0]
qtde_de_vendas = qtde_de_vendas.text
valor_bruto = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[2]/div/div/div[3]/div[2]'
)[0]
valor_bruto = valor_bruto.text
valor_descontado = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[3]/div/div/div[1]/div[2]'
)[0]
valor_descontado = valor_descontado.text
valor_liquido = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[3]/div/div/div[2]/div[2]'
)[0]
valor_liquido = valor_liquido.text
taxa_de_embarque = driver.find_elements_by_xpath(
'//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_resumoCreditoControl_sumario"]/div/div[3]/div/div/div[3]/div[2]'
)[0]
taxa_de_embarque = taxa_de_embarque.text
resumo = {
'data_da_venda': data_da_venda,
'data_do_processamento': data_do_processamento,
'resumo_vendas': resumo_vendas,
'bandeira': bandeira,
'qtde_de_vendas': qtde_de_vendas,
'valor_bruto': valor_bruto,
'valor_descontado': valor_descontado,
'valor_liquido': valor_liquido,
'taxa_de_embarque': taxa_de_embarque
}
itens_vencimetos = {}
tbody = '//*[@id="tbVencimento"]/tbody/tr'
dados = 0
for row in driver.find_elements_by_xpath(tbody):
data_recebimento = row.find_elements_by_tag_name("td")[0].text
numero_estabelecimento = row.find_elements_by_tag_name("td")[1].text
ordem_credito = row.find_elements_by_tag_name("td")[2].text
status_do_resumo = row.find_elements_by_tag_name("td")[3].text
data_da_antecipacao = row.find_elements_by_tag_name("td")[4].text
valor_liquido = row.find_elements_by_tag_name("td")[5].text
vencimento = {
'data_de_recebimento': data_recebimento,
'numero_do_estabelecimento': numero_estabelecimento,
'ordem_de_credito': ordem_credito,
'status_do_resumo': status_do_resumo,
'data_da_antecipacao': data_da_antecipacao,
'valor_liquido': valor_liquido
}
itens_vencimetos[dados] = vencimento
dados += 1
itens_vendas = {}
tbody = '//*[@id="tbVendasAceitas"]/tbody/tr'
dados = 0
for row in driver.find_elements_by_xpath(tbody):
comprovante = row.find_elements_by_tag_name("td")[1].text
nro_cartao = row.find_elements_by_tag_name("td")[2].text
cartao = row.find_elements_by_tag_name("td")[3].text
pais = row.find_elements_by_tag_name("td")[4].text
dt_venda = row.find_elements_by_tag_name("td")[5].text
hora = row.find_elements_by_tag_name("td")[6].text
valor_bruto = row.find_elements_by_tag_name("td")[7].text
parcelas = row.find_elements_by_tag_name("td")[8].text
nro_estabelecimento = row.find_elements_by_tag_name("td")[9].text
tipo_de_captura = row.find_elements_by_tag_name("td")[10].text
venda_cancelada = row.find_elements_by_tag_name("td")[11].text
venda = {
'nº do comprovante de venda (NSU)': comprovante,
'nº cartão ou ID carteira digital': nro_cartao,
'cartão virtual(tokenização)': cartao,
'país de origem': pais,
'data da venda': dt_venda,
'hora': hora,
'valor bruto': valor_bruto,
'qtde de parcelas': parcelas,
'nº estabelec.': nro_estabelecimento,
'tipo de captura': tipo_de_captura,
'venda cancelada': venda_cancelada
}
itens_vendas[dados] = venda
dados+=1
# driver.close()
# return
# Write JSON file
data = {
'resumo de vendas - cartões de crédito': resumo,
'vencimentos': itens_vencimetos,
'comprovantes de vendas aceitos': itens_vendas,
}
# write(data)
# driver.back()
driver.execute_script("window.history.go(-1)")
# driver.find_element_by_xpath(
# '//*[@id="ctl00_m_g_319a4cca_3fcc_42ba_97b4_c15b0874c184_ctl00_btnVoltar"]'
# ).click()
if __name__ == "__main__":
driver = webdriver.Chrome()
main(driver)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment