30000毫秒后超时

1 投票
3 回答
3348 浏览
提问于 2025-04-16 02:38

当我使用SeleniumRC的时候,有时候会遇到一个错误,但有时候又不会。我猜这个问题和wait_for_page_to_load()的等待时间有关,但我不知道需要等多久。

错误信息:

Exception: Timed out after 30000ms
File "C:\Users\Herta\Desktop\test\newtest.py", line 9, in <module>
  sel.open(url)
File "C:\Users\Herta\Desktop\test\selenium.py", line 764, in open
  self.do_command("open", [url,])
File "C:\Users\Herta\Desktop\test\selenium.py", line 215, in do_command
  raise Exception, data

这是我的程序:

from selenium import selenium

url = 'http://receptome.stanford.edu/hpmr/SearchDB/getGenePage.asp?Param=4502931&ProtId=1&ProtType=Receptor#'

sel = selenium('localhost', 4444, '*firefox', url)
sel.start()
sel.open(url)
sel.wait_for_page_to_load(1000)
f = sel.get_html_source()
sav = open('test.html','w')
sav.write(f)
sav.close()
sel.stop()

3 个回答

0

我遇到过这个问题,是因为Windows防火墙阻止了Selenium服务器的运行。你有没有试过在防火墙中添加一个例外?

1

在自动化用户界面(UI)页面时,时间管理是个大问题。你需要在合适的时候使用超时设置,并给某些事件留出足够的时间。我看到你有

sel.open(url)
sel.wait_for_page_to_load(1000)

在调用 sel.open 之后使用 sel.wait_for_page_to_load 命令是多余的。所有的 sel.open 命令本身就有内置的等待机制。这可能是你遇到问题的原因,因为 selenium 在执行 sel.open 命令时已经在等待页面加载。然后又让 selenium 再次等待页面加载,但此时页面还没有加载完成,所以就会报错。

不过,这种情况不太可能,因为错误信息是出现在 sel.open 命令上。Wawa 上面的回答可能是你最好的选择。

1

“在30000毫秒后超时”的消息是因为你调用了 sel.open(url) 这个函数,它使用了 Selenium 默认的超时时间。你可以试着把这个时间调长,使用 sel.set_timeout("timeout") 来设置。我建议你可以先试试60秒,如果60秒还不行,就再把超时时间调长。另外,确保你能正常打开这个页面。

from selenium import selenium

url = 'http://receptome.stanford.edu/hpmr/SearchDB/getGenePage.asp?Param=4502931&ProtId=1&ProtType=Receptor#'

sel = selenium('localhost', 4444, '*firefox', url)
sel.set_timeout('60000')
sel.start()
sel.open(url)
sel.wait_for_page_to_load(1000)
f = sel.get_html_source()
sav = open('test.html','w')
sav.write(f)
sav.close()
sel.stop()

撰写回答