【问题标题】:Selenium RC: how to capture/handle error?Selenium RC:如何捕获/处理错误?
【发布时间】:2009-11-05 03:40:39
【问题描述】:

我的测试使用 Selenium 通过 HTTP 代理(下面的工作脚本)循环遍历 URL 的 CSV 列表。当我观察脚本运行时,我可以看到大约 10% 的调用产生“代理错误:502”(“Bad_Gateway”);但是,我的包罗万象的“except Exception”子句没有捕获这些错误——即:不是在“output.csv”的适当行中写入“error”,而是将它们传递给 else 子句并生成一个简短的一段 html 开头:“代理错误:502 从服务器读取失败:未知错误。”此外,如果我收集所有返回 502 的 URL 并重新运行脚本,它们都通过了,这让我相信这是一个零星的网络路径问题。

问题:是否可以使脚本识别 502 错误,休眠一分钟,然后重试 URL,而不是转到列表中的下一个 URL?

我能想到的唯一替代方法是在“get_html_source”之后应用 re.search("Proxy error: 502") 作为捕获错误调用的一种方式。然后,如果 RE 匹配,让脚本休眠一分钟,然后在产生 502 的 URL 上重试 'sel.open(row[0]'。任何建议将不胜感激。谢谢!

#python 2.6
from selenium import selenium
import unittest, time, re, csv, logging

class Untitled(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", "http://baseDomain.com")
        self.selenium.start()
        self.selenium.set_timeout("60000")

    def test_untitled(self):
        sel = self.selenium
        spamReader = csv.reader(open('ListOfSubDomains.csv', 'rb'))
        for row in spamReader:
            try:
                sel.open(row[0])
            except Exception:
                ofile = open('output.csv', 'ab')
                ofile.write("error" + '\n')
                ofile.close()
            else:
                time.sleep(5)
                html = sel.get_html_source()
                ofile = open('output.csv', 'ab')
                ofile.write(html.encode('utf-8') + '\n')
            ofile.close()

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
     unittest.main()

【问题讨论】:

    标签: python error-handling csv selenium loops


    【解决方案1】:

    我认为您提出的替代方案是可以的。而不是 get_html_source,您可以使用 captureNetworkTraffic 函数来获取 HTTP 标头。这样会更安全,因为 502 页面可以更改。

    请注意,selenium python 包装器的 captureNetworkTraffic 中有一个可以被黑客攻击的错误。见:http://jira.openqa.org/browse/SRC-758

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 2011-01-12
      • 2011-03-04
      • 2019-06-30
      • 2022-01-08
      • 2013-01-05
      • 1970-01-01
      相关资源
      最近更新 更多