0

Amigos.

Estou desenvolvendo um código para acessar o site da Anbima, preencher os campos e fazer o download do txt que é gerado.

Estou há alguns dias buscando uma solução para esse problema. Até o momento, descobri que a sessão onde se inputa as informações é um iframe gerado de outra página. O código que desenvolvi até o momento é este:

from selenium import webdriver

#Variables

url = 'http://www.anbima.com.br/reune/reune.asp'
path_phantom = 'C:\\Users\\TBMEPYG\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'

#Processing

driver = webdriver.PhantomJS(executable_path= path_phantom)

data = driver.find_element_by_name('Dt_Ref')
data.clear()
data.send_keys('21/08/2017')
driver.quit()

Notem que a ideia e preencher o primeiro formulário de data, porém recebo o seguinte erro, informando que não foi encontrado o elemento.

Traceback (most recent call last):
  File "C:\Users\TBMEPYG\Desktop\beta_anbima.py", line 16, in <module>
    data = driver.find_element_by_name('Dt_Ref')
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 426, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
    'value': value})['value']
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with name 'Dt_Ref'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62051","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"name\", \"value\": \"Dt_Ref\", \"sessionId\": \"855174e0-8e52-11e7-84d4-0792b107ed82\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/855174e0-8e52-11e7-84d4-0792b107ed82/element"}}
Screenshot: available via screen

Alguma ideia do que pode estar acontecendo?

Estre projeto está sendo desenvolvido no meu trabalho, que tem altas restrições com acessos externos. Informo que não teria flexibilidade para mudar o webdriver para Chrome ou Firefox.

Fico no aguardo.

Edit 2:

Encontrei um erro no código, não estava dando o comando get. Fazendo esse ajuste, um novo erro surge. Segue o novo código

from selenium import webdriver


url = 'http://www.anbima.com.br/reune/reune.asp'
path_phantom = 'C:\\Users\\TBMEPYG\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'

#Processing

driver = webdriver.PhantomJS(executable_path= path_phantom)
driver.get(url)

#Encontrando o nome do field

name = driver.find_element_by_name("Dt_Ref")

driver.quit()

E o erro encontrado agora é diferente:

Traceback (most recent call last):
  File "C:\Users\TBMEPYG\Desktop\beta_anbima.py", line 18, in <module>
    name = driver.find_element_by_name("Dt_Ref")
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 426, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
    'value': value})['value']
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: {"request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:53945","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"name\", \"value\": \"Dt_Ref\", \"sessionId\": \"89ca1270-8e57-11e7-a14f-5d3312ce36ce\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/89ca1270-8e57-11e7-a14f-5d3312ce36ce/element"}}
Screenshot: available via screen

1 Resposta 1

0

Confira o nome do elemento que quer acessar. O elemento Dt_Ref não existe segundo o erro:

 data = driver.find_element_by_name('Dt_Ref')

"errorMessage":"Unable to find element with name 'Dt_Ref'",
3
  • Lucas, ele se encontra no código da página: <input type="hidden" name="Dt_RefAnt" value="31/08/2017" maxlength="10" size="10"> 31/08/2017 às 13:55
  • O nome dele é Dt_RefAnt 31/08/2017 às 13:55
  • Copiei a linha errada. Temos Dt_Ref também <input type="text" name="Dt_Ref" value="31/08/2017" maxlength="10" size="10" class="form_data" 31/08/2017 às 13:56

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .