【问题标题】:Simplify selenium output in python简化 python 中的 selenium 输出
【发布时间】:2015-01-20 17:25:17
【问题描述】:

我希望 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

【问题讨论】:

标签: python selenium xpath exception-handling selenium-webdriver


【解决方案1】:

在测试过程中抛出了一个错误,这是一个错误回溯。

你可以做的是:

当此变量设置为整数值时,它确定 最大级别的回溯信息打印时一个 发生未处理的异常。默认值为 1000。当设置为 0 或 更少,所有回溯信息都被抑制,只有异常 打印类型和值。

  • 拥有自己的 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)

【讨论】:

  • 谢谢,这可以摆脱回溯,但在 NoSuchElementException 之后我仍然收到 json 响应
  • @programiss 谢谢,你能分享一下当前的输出吗?
  • @programiss 知道了,你用的是 PhantomJS 驱动,对吧?
  • 我相信是的,这就是我目前拥有的self.driver = webdriver.Remote( command_executor='http://10.0.0.100:49044/wd/hub', desired_capabilities=DesiredCapabilities.PHANTOMJS) self.driver.set_window_size(1400, 900)
  • @programiss caused by Request... 部分来自PhantomJS。更新了代码,只留下了errorMessage,检查一下。谢谢。 (您可能还需要将PhantomJSselenium 升级到最新版本才能正常工作)。
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 2019-06-23
  • 2022-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多