简体   繁体   中英

AssertionError - Selenium/Python

I am creating a Python script with Selenium. 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: '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.

I would check your second parameter to self.assertEqual and make sure that you're selecting the right feature.

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

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). You might not be selecting the right element with that XPath query. Maybe a CSS query would be best. You can read about element selectors in the Selenium documentation .

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. This might not work as expected on special characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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