Selenium WebDriver error accent

-2

I'm using Selenium WebDriver to realify test with python

when executing this line:

mov.find_elements_by_xpath("td")[3].text.encode('utf-8')

I have the answer:

{'descricao': 'PROTOCOLIZADA PETI\xc3\x87\xc3\x83O'},

When should you:

{'descricao': 'PROTOCOLIZADA PETIÇÃO'},

Full Code

#encoding: utf-8
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support import ui
from app.browser_automation.browser_automation import BrowserAutomation
import time
import CONST
from datetime import datetime

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import ui

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class BrowserAutomationTrt19(BrowserAutomation):

    def page_is_loaded(self, driver):
        time.sleep(2)
        return True #driver.find_element_by_id("Div_HeaderMedium") != None

    # consultas dos processo  na base afim de procurar no portal
    def make_consulta(self, numero):

        #preencher os elementos da consulta
        #dividir os numero em array
        partes_numero_processo = self.get_partes_do_numero(numero)

        #preencher os campos no formulario da página

        self.browser.find_element_by_xpath("//input[contains(@id,'seq')]").send_keys(partes_numero_processo[0])
        self.browser.find_element_by_xpath("//input[contains(@id,'dig')]").send_keys(partes_numero_processo[1])
        self.browser.find_element_by_xpath("//input[contains(@id,'ano')]").send_keys(partes_numero_processo[2])
        self.browser.find_element_by_xpath("//input[contains(@id,'org')]").send_keys(partes_numero_processo[5])

        #clicar no butão buscar
        self.browser.find_element_by_xpath("//input[contains(@class,'btnBuscar')]").click()
        #atualizar tela
        self.browser.refresh

        #se nao encontrar processo
        #if not self.processo_naoencontrado():
            #wait = ui.WebDriverWait(self.browser, 1000000000)
        #else:
            #CONST.PROCESSO_ENCONTRADO = False

        return True

    #se nao encontra processo
    def processo_naoencontrado(self):

        try:
            return "Nenhum registro encontrado!" in self.browser.find_element_by_id('idDivBlocoMensagem').text
        except:
            return False

    #obtendo as movimentacoes
    def read_movimentacoes(self,processo):
        movimentacoes = []
        time.sleep(5)

        #clicar no link <a>     
        self.browser.find_element_by_xpath("//div[contains(@class,'row-fluid')]/p[contains(@class,'lead')]/a").click()
        time.sleep(2)

        print 'Lendo Movimentacoes...1'
        moviments = self.browser.find_elements_by_xpath("//table[contains(@id,'tableMovimentacoes1inst')]/tbody/tr")

        print "teste 0"
        for mov in moviments:
            data_hora = mov.find_elements_by_xpath("td")
            data_hora = data_hora[0].text + " " + data_hora[1].text + ":00"
            data_mov = datetime.strptime(data_hora.strip(), '%d/%m/%Y %H:%M:%S')

            if processo['ultima_movimentacao'] <= data_mov:

                fase_movimentacao = mov.find_elements_by_xpath("td")[3].text.encode('utf-8')

                mov = {'data': data_hora,
                    'faseMovimentacao': {'descricao': fase_movimentacao}
                    }

                if mov not in movimentacoes:
                    movimentacoes.append(mov)
                    # print mov
            else:
                print "teste3"
                return movimentacoes

        print "teste4"
        time.sleep(2)
        return movimentacoes

Image of the error

  

phase_movement=  mov.find_elements_by_xpath("td") [3] .text.decode ('utf-8') File   "C: \ Python27 \ Lib \ encodings \ utf_8.py", line 16, in decode       return codecs.utf_8_decode (input, errors, True) UnicodeEncodeError: 'ascii' codec can not encode characters in position   6-7: ordinal not in range (128)

    
asked by anonymous 28.08.2018 / 00:50

1 answer

2

The problem is not in Selenium, probably the problem is in the site that you are trying to encode or decode to utf8, there may simply be invalid characters, which probably on the page you try to access should be displayed as:

  • Or something similar to é (representing it) or ã (which represents ã)

You may not even need encode in mov.find_elements_by_xpath("td")[3].text.encode('utf-8') since it is likely that the site is already in utf-8 as well.

I usually use iso-8859-1 / windows-1252 or utf-8, I believe your intent is to convert to a string, in case you could probably do this:

fase_movimentacao = b"abc"

try:
    minha_str = fase_movimentacao.decode('utf-8')
except ValueError:
    minha_str = fase_movimentacao.decode('iso-8859-1')

print(minha_str)

In your code it would look something like:

fase_movimentacao = mov.find_elements_by_xpath("td")[3].text

try:
    minha_str = fase_movimentacao.decode('utf-8')
except ValueError:
    minha_str = fase_movimentacao.decode('iso-8859-1')

mov = {'data': data_hora,
    'faseMovimentacao': {'descricao': minha_str }
}

But I can not say if it will suit everything, especially if you have coding problems at the source site.

    
28.08.2018 / 15:56