gpt4 book ai didi

python - 简化python中的selenium输出

转载 作者:太空宇宙 更新时间:2023-11-04 01:10:29 25 4
gpt4 key购买 nike

我想让 selenium 为我目前保存到输出日志的 selenium 测试输出一个更简单和直接的输出

======================================================================
ERROR: test3_AnswerTest (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 98, in test3_FashionTest
driver.find_element_by_xpath("//a[contains(.,'Answer')]").click()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 230, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
{'using': by, 'value': value})['value']
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Error Message => 'Unable to find element with xpath '//a[contains(.,'Ass')]''
caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"106","Content-Type":"application/json;charset=UTF-8","Host":"10.0.0.100:49044","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"fd4e8db0-a0c6-11e4-9624-f33660aa508e\", \"value\": \"//a[contains(.,'Ass')]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/fd4e8db0-a0c6-11e4-9624-f33660aa508e/element"}
Screenshot: available via screen

是否可以将此输出简化为类似这样的内容?

NoSuchElementException: Message: Error Message => 'Unable to find element with xpath '//a[contains(.,'Answer')]''

当前错误信息是

Traceback (most recent call last):
NoSuchElementException: Message: Error Message => 'Unable to find element with xpath '//a[contains(.,'Answer')]''
caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"106","Content-Type":"application/json;charset=UTF-8","Host":"10.0.0.100:49044","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"12e538b0-a0e3-11e4-af45-ff6a851f3fb0\", \"value\": \"//a[contains(.,'Ass')]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/12e538b0-a0e3-11e4-af45-ff6a851f3fb0/element"}
Screenshot: available via screen

最佳答案

测试期间出现错误,这是错误回溯。

你可以做的是:

When this variable is set to an integer value, it determines the maximum number of levels of traceback information printed when an unhandled exception occurs. The default is 1000. When set to 0 or less, all traceback information is suppressed and only the exception type and value are printed.

  • 拥有自己的 selenium webdriver 错误处理程序 以清除 selenium 错误回溯(tracebacklimit 不会影响它 - 您需要像这样手动执行)

工作示例:

import unittest
import sys

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.remote.errorhandler import ErrorHandler


class MyHandler(ErrorHandler):
def check_response(self, response):
try:
super(MyHandler, self).check_response(response)
except NoSuchElementException as e:
e.stacktrace = None
# PhantomJS specific line:
e.msg = json.loads(e.msg)['errorMessage']
raise


class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.error_handler = MyHandler()

def test(self):
self.driver.get("http://google.com")
self.driver.find_element_by_id('illegal')

def tearDown(self):
self.driver.quit()


if __name__ == "__main__":
sys.tracebacklimit = 0
unittest.main()

这是控制台上的内容:

$ python test.py
E
======================================================================
ERROR: test (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"illegal"}'

----------------------------------------------------------------------
Ran 1 test in 5.401s

FAILED (errors=1)

关于python - 简化python中的selenium输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28051501/

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