python startswitch_新手python+webdriver+selenium使用unittest单元测试框架,无法正常switch_to.window...

最近在学习unittest单元测试框架,之前调试好的测试脚本,发现放在这个框架中,无法正常switch_to.window,总是报错无法定位到元素,个人感觉是因为窗口梅正常切换过去,新人求指教,谢谢,代码如下:

main.py

import time

import unittest

import auto_test

suite = unittest.TestSuite()

suite.addTest(auto_test.Python_autotest('get_browser'))

suite.addTest(auto_test.Python_autotest('search'))

suite.addTest(auto_test.Python_autotest('user_login'))

suite.addTest(auto_test.Python_autotest('user_logout'))

if __name__ == '__main__':

runner = unittest.TextTestRunner()

runner.run(suite)

auto_test.py

from selenium import webdriver

import time

import unittest

class Python_autotest(unittest.TestCase):

def setUp(self,driver = webdriver.Firefox()):

print('Test is start')

self.driver = driver

def get_browser(self):

self.driver.get("https://www.baidu.com/")

def search(self):

try:

self.driver.find_element_by_xpath('//*[@id="kw"]').send_keys('segmentfault')

self.driver.find_element_by_xpath('//*[@id="su"]').click()

except Exception:

print('No found everything, sorry!')

else:

time.sleep(1)

self.driver.find_element_by_xpath("/html/body/div[1]/div[5]/div[1]/div[3]/div[1]/h3/a").click()

def user_login(self):

self.driver.switch_to.window(self.driver.window_handles[-1])

time.sleep(3)

self.driver.find_element_by_xpath('/html/body/div[4]/div/div/div[1]/div[2]/a[1]').click()#点击页面中的前端菜单栏

def user_logout(self):

self.driver.quit()

def tearDown(self):

print('Test is finish')

if __name__ == '__main__':

unittest.main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值