Timeout when using click() webdriver selenium function Python

白昼怎懂夜的黑 提交于 2019-12-24 09:24:57

问题


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:


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') 


来源:https://stackoverflow.com/questions/46939451/timeout-when-using-click-webdriver-selenium-function-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!