简体   繁体   English

AssertionError-Selenium / Python

[英]AssertionError - Selenium/Python

I am creating a Python script with Selenium. 我正在用Selenium创建一个Python脚本。 I want to run a specific test that checks the default text of a textbox when the page loads up. 我想运行一个特定的测试,以在页面加载时检查文本框的默认文本。 Below is my code....... 下面是我的代码.......

try: 
                    self.assertEqual("Search by template name or category..", sel.get_text("//table[@id='pluginToolbarButton_forms']/tbody/tr[2]/td[2]/em"))
                    logging.info('      PASS:  text box text is correct')
                except Exception:
                    logging.exception('     FAIL: text box text is incorrect')

Here is my error...... 这是我的错误...

            self.assertEqual("Search by template name or category..", sel.get_text("//table[@id='pluginToolbarButton_forms']/tbody/tr[2]/td[2]/em"))
  File "C:\Python27\lib\unittest\case.py", line 509, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:\Python27\lib\unittest\case.py", line 502, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 'Search by template name or category..' != u'Submitter Requests'

Am I using the wrong function? 我使用了错误的功能吗?

Your AssertionError states that the assertion you tried (that's the self.assertEqual(...) in your first code example) failed: 您的AssertionError指出您尝试的断言self.assertEqual(...)在第一个代码示例中为self.assertEqual(...) )失败了:

AssertionError: 'Search by template name or category..' != u'Submitter Requests'

This assertion explains that the string 'Search by template name or category' is different from 'Submitter Requests' , which is correct ... the strings are , in fact, different. 该断言说明字符串'Search by template name or category''Submitter Requests'不同,这是正确的……实际上,这些字符串不同的。

I would check your second parameter to self.assertEqual and make sure that you're selecting the right feature. 我将检查您的第二个参数self.assertEqual ,并确保您选择了正确的功能。

看起来您使用的是正确的功能,但是也许您没有以正确的方式运行测试。

The problem seems to be that you are not selecting the right element to compare with. 问题似乎是您没有选择正确的元素进行比较。 You are basically telling the program to match that "Search by template name or category.." is equal to the contents of whatever is in: 您基本上是在告诉程序匹配“按模板名称或类别搜索。”等于以下内容:

//table[@id='pluginToolbarButton_forms']/tbody/tr[2]/td[2]/em

Apparently, the contents are "Submitter Requests", ie not what you would expect, so the test fails (as it should). 显然,内容是“ Submitter请求”,即不是您期望的内容,因此测试失败(应该如此)。 You might not be selecting the right element with that XPath query. 您可能没有通过该XPath查询选择正确的元素。 Maybe a CSS query would be best. 也许CSS查询是最好的。 You can read about element selectors in the Selenium documentation . 您可以在Selenium文档中阅读有关元素选择器的信息

Keep an eye open for a pitfall as well: the text returned by Selenium is a Unicode object , and you are comparing it against a string. 还要注意一个陷阱:Selenium返回的文本是Unicode对象 ,您正在将其与字符串进行比较。 This might not work as expected on special characters. 对于特殊字符,这可能无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM