Timeout when using click() webdriver selenium func

2019-08-20 20:00发布

This is my first web scraping project and I'm using selenium webdriver with Python in order to dynamically generate some csv files after choosing a few options on a website (though I'm not there yet).

However, I'm facing an unexpected timeout when the execution reaches a button click(). The click is performed but it gets stuck in there and does not continue the execution till the timeout.

Any clues on how to solve that?

Thanks!!

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time


driver = webdriver.Firefox()
driver.get('http://www8.receita.fazenda.gov.br/SimplesNacional/Aplicacoes/ATBHE/estatisticasSinac.app/Default.aspx')
driver.find_element_by_id('ctl00_ctl00_Conteudo_AntesTabela_lnkOptantesPorCNAE').click()
Select(driver.find_element_by_id("ctl00_ctl00_Conteudo_AntesTabela_ddlColuna")).select_by_visible_text("Município")
filtro_uf =     driver.find_element_by_id('ctl00_ctl00_Conteudo_AntesTabela_btnFiltros')

for i in range (1, 28):
    filtro_uf.click()
    uf = Select(driver.find_element_by_id("ctl00_ctl00_Conteudo_AposTabela_ddlUf"))
    uf.options[i].click()
    time.sleep(2)
    driver.find_element_by_id('chkTodosMunicipios').click()
    time.sleep(2)
    driver.find_element_by_xpath("//*[contains(text(),'Ok')]").click()
    time.sleep(2)

# Here is where my code get stuck and gets a timeout
    driver.find_element_by_id('ctl00_ctl00_Conteudo_AntesTabela_btnExibir').click()

The error I get:

Traceback (most recent call last):      
File "/home/hissashi/Desktop/Python3/WS_SINAC/download_SINAC.py", line 22, in <module>   driver.find_element_by_id('ctl00_ctl00_Conteudo_AntesTabela_btnExibir').click()   
  File "/home/hissashi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)    
  File "/home/hissashi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
    return self._parent.execute(command, params)   
  File "/home/hissashi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)   
  File "/home/hissashi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response   
raise exception_class(message, screen, stacktrace)
**selenium.common.exceptions.TimeoutException: Message: Timeout loading page after 300000ms**

1条回答
萌系小妹纸
2楼-- · 2019-08-20 20:43

I've found a workaround for the problem.

Apparently, the click() function blocks the code until the page is "completely" loaded. However, for some reason, the page keeps loading forever (without anything else to load) and it holds my code till it reaches the timeout limit.

Instead of using click, I've changed it to key ENTER and the page still keeps loading forever but it doesn't hold the code anymore.

#FROM CLICK
driver.find_element_by_id('ctl00_ctl00_Conteudo_AntesTabela_btnExibir').click()

#TO SENDING ENTER (ue007)
driver.find_element_by_id('ctl00_ctl00_Conteudo_AntesTabela_btnExibir').send_keys(u'\ue007') 
查看更多
登录 后发表回答