Victor Medeiros
Victor Medeiros

Reputation: 37

Selenium Webdriver with Python - Not able to click in a web application using Selenium Web driver

Can't Click on the button 'consultar' and select the line I want

import time
import xlrd  # importando a biblioteca
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import zipfile

inicio = time.time()

# datapagamento = time.strftime(input('Qual a data de pagamento?'))
# data = time.strftime(input('Qual o mês de referência?'))
datainicial = "042019"


# Pegando nome do mês para salvar
if datainicial.startswith('01'):
    mes = 'Janeiro'
elif datainicial.startswith('02'):
    mes = 'Fevereiro'
elif datainicial.startswith('03'):
    mes = 'Março'
elif datainicial.startswith('04'):
    mes = 'Abril'
elif datainicial.startswith('05'):
    mes = 'Maio'
elif datainicial.startswith('06'):
    mes = 'Junho'
elif datainicial.startswith('07'):
    mes = 'Julho'
elif datainicial.startswith('08'):
    mes = 'Agosto'
elif datainicial.startswith('09'):
    mes = 'Setembro'
elif datainicial.startswith('10'):
    mes = 'Outubro'
elif datainicial.startswith('11'):
    mes = 'Novembro'
elif datainicial.startswith('12'):
    mes = 'Dezembro'
else:
    print('Insira data iniciando do dia 01')

workbook = xlrd.open_workbook('teste.xlsx')  # Escolhe o arquivo a ser lido.
worksheet = workbook.sheet_by_index(1)  # Escolha a aba a ser lida.
# Pega as informações do Excel
i = 0
while (i in range(worksheet.nrows)):
    #cnpj = worksheet.cell_value(rowx=i, colx=0)
    #senha = worksheet.cell_value(rowx=i, colx=1)
    #empresa = worksheet.cell_value(rowx=i, colx=2)

    cnpj = '13177807000146'
    senha = 'qualita@2018'
    i = i + 1

    driver = webdriver.Chrome()
    # driver.maximize_window()
    driver.get("https://directa.natal.rn.gov.br/")
    # Logando
    driver.switch_to.frame(driver.find_element_by_name("mainsystem"))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "usuario"))).send_keys(cnpj)
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "senha"))).send_keys(str(senha))
    time.sleep(2)
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "button.btn#acessar"))).click()
    # Nota natalense
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "mainsystem")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'limenu9'))).click()
    time.sleep(1)
    # operações
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#limenu9 > div > a:nth-child(3) > span:nth-child(1)")))
    element = driver.find_element(By.CSS_SELECTOR, '#limenu9 > div > a:nth-child(3) > span:nth-child(1)')
    webdriver.ActionChains(driver).move_to_element(element).perform()
    # Emissão de DAM
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="formsmenu14"]/li[5]/a'))).click()
    # Trocando frame
    time.sleep(1)
    driver.switch_to.frame(0)
    driver.switch_to.frame(0)
    # Selecionando empresa
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable( (By.XPATH, '/html/body/form/div/div[2]/div[2]/div[10]/div[2]/div/div/table/tbody/tr/td'))).click()
    WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/table/tbody/tr[2]/td/select/option[2]'))).click()
    # Consultar
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="lay"]/div[2]/div[2]/div[6]/div/table/tbody/tr'))).click()
    # selecionando o mês do DAM
    driver.find_elements_by_xpath('//div[contains(text(), "{}") and @class="inner"]'.format(datainicial)).doubleClick()


fim = time.time()
duracao = fim - inicio
print('O programa rodou em: {} e foram baixadas {} empresas'.format(duracao, i))

Upvotes: 0

Views: 80

Answers (1)

Dmitri T
Dmitri T

Reputation: 168082

  1. Your /html/body/form/div/div[2]/div[2]/div[10]/div[2]/div/div/table/tbody/tr/td XPath expression is not the best option of identifying web elements, if you want to click the link with Consultar text it's better to amend your selector to look like:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Consultar')))
    

    if you still want to use XPath - you can use text() function like:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Consultar']")))
    
  2. Sometimes DOM elements cannot be clickable by Selenium's click() function, in this case you can work it around by executing a JavaScript call like:

     driver.execute_script("arguments[0].click()", driver.find_element_by_link_text("Consultar"))
    

Upvotes: 0

Related Questions