gpt4 book ai didi

python - Selenium RC : how to capture/handle error?

转载 作者:行者123 更新时间:2023-12-03 08:36:33 24 4
gpt4 key购买 nike

我的测试使用 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()

最佳答案

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

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

关于python - Selenium RC : how to capture/handle error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1678195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com