使用click()Webdriver Selenium函数Python时超时 [英] Timeout when using click() webdriver selenium function Python

查看:228
本文介绍了使用click()Webdriver Selenium函数Python时超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个Web抓取项目,我在Python上使用了Selenium Webdriver,以便在选择网站上的一些选项后动态生成一些csv文件(尽管我还没有)。

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

但是,当执行到达按钮click()时,我面临着意外的超时。单击会执行,但会卡在其中,并且直到超时都不会继续执行。

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?

谢谢!

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

我得到的错误:

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**


推荐答案

我找到了解决方法

显然,click()函数会阻塞代码,直到页面完全加载为止。但是,由于某种原因,该页面会一直永久加载(无需加载其他任何内容),并保留我的代码,直到达到超时限制为止。

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.

而不是使用click,我已将其更改为键ENTER,并且页面仍然永久加载,但不再保存代码。

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

这篇关于使用click()Webdriver Selenium函数Python时超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆