密码正确的会通过,密码错误报错

密码正确的会通过,密码错误报错

import unittest
from selenium import webdriver
from time import sleep

from pages.first_page import FirstPage
from pages.login_page import LoginPage
from ddt import ddt,data
from xlrd import open_workbook

def getexcel():
excelFile = open_workbook('d:/data.xlsx')
sheet = excelFile.sheet_by_index(0)
rowNuber = sheet.nrows
data_list = []
for i in range(1,rowNuber):
data_list.append(sheet.row_values(i))
return data_list

@ddt
class TestTravel(unittest.TestCase):
def setUp(self):
self.dr = webdriver.Chrome()
self.dr.maximize_window()
self.dr.implicitly_wait(10)

@data(*getexcel())
def test_login_ddt(self,data):
username,password = tuple(data)
firstpage = FirstPage(driver=self.dr,path='http://django.t.mukewang.com/')
loginpage = LoginPage(driver=self.dr)
firstpage.cross_to_login()
sleep(2)
loginpage.login(username,password)


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

if __name__ == '__main__':
unittest.main()

C:\Users\王八蛋\PycharmProjects\travel_test\venv\Scripts\python.exe "D:\PyCharm Community Edition 2020.2.3\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --target test_travel_ddt.TestTravel
Testing started at 20:55 ...
Launching unittests with arguments python -m unittest test_travel_ddt.TestTravel in C:\Users\王八蛋\PycharmProjects\travel_test



Ran 2 tests in 36.406s

FAILED (errors=1)

Error
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2288.0_x64__qbz5n2kfra8p0\lib\unittest\case.py", line 60, in testPartExecutor
yield
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2288.0_x64__qbz5n2kfra8p0\lib\unittest\case.py", line 676, in run
self._callTestMethod(testMethod)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2288.0_x64__qbz5n2kfra8p0\lib\unittest\case.py", line 633, in _callTestMethod
method()
File "C:\Users\王八蛋\PycharmProjects\travel_test\venv\lib\site-packages\ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "C:\Users\王八蛋\PycharmProjects\travel_test\test_travel_ddt.py", line 33, in test_login_ddt
loginpage.login(username,password)
File "C:\Users\王八蛋\PycharmProjects\travel_test\pages\login_page.py", line 25, in login
self.back_to_firstpage().click()
File "C:\Users\王八蛋\PycharmProjects\travel_test\pages\login_page.py", line 19, in back_to_firstpage
return self.by_xpath('//*[@id="app"]/div[1]/div[5]/div[1]')
File "C:\Users\王八蛋\PycharmProjects\travel_test\pages\base_page.py", line 16, in by_xpath
return self.driver.find_element_by_xpath(xpath)
File "C:\Users\王八蛋\PycharmProjects\travel_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\王八蛋\PycharmProjects\travel_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\王八蛋\PycharmProjects\travel_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\王八蛋\PycharmProjects\travel_test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="app"]/div[1]/div[5]/div[1]"}
(Session info: chrome=89.0.4389.114)




Assertion failed

Process finished with exit code 1


Assertion failed

Assertion failed


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕美 2021-04-15 10:24:18

同学,你好!报错是找不到相应的元素,可能元素还没有加载出来。

运行同学的代码没有报错,同学可重新执行程序,也可以设置等待时间,即页面中的所有元素设置加载时间,即整个页面的加载时间;

例:

http://img1.sycdn.imooc.com//climg/6077a3ca0969bc9407360269.jpg

祝学习愉快!

  • 提问者 weixin_慕粉0071985 #1

    已经设置了全局等待时间,会到这一步验证密码失败,然后报错http://img1.sycdn.imooc.com//climg/6078209f0915925b17970991.jpg

    2021-04-15 19:17:11
  • 提问者 weixin_慕粉0071985 #2

    密码错误会报错,正确就会通过

    2021-04-15 19:17:52
  • 同学,你好!错误信息提示元素//*[@id="app"]/div[1]/div[5]/div[1]没有定位到,这是"首页"按钮,在登录失败页面,是没有这个按钮的,只有登录成功后才可以点击首页。同学检查一下first_page.cross_to_login()方法,是否有点击首页的操作,也可将代码粘贴在此处,便于老师定位问题。

    祝:学习愉快!

    2021-04-16 11:30:23
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师