Python+Selenium+Unittest编写超链接点击测试用例

测试功能:博客园首页网站分类的一级菜单链接和二级菜单链接的点击。

遇到的问题:

  1.循环点击二级菜单时,点击了一个一级菜单下的第一个二级菜单后,页面会刷新,再定位同一个一级菜单次下的第二个二级菜单时,

会报错:找不到第二个二级菜单,这时需要对一级菜单重新定位赋值;

  2.当一级菜单下的二级菜单太多时,在页面的不可见区域,这时会报错:element not interactable,这时需要滑动滚动条。

 

# coding=utf-8
from selenium import webdriver
import unittest
from selenium.webdriver import ActionChains
from selenium.common.exceptions import NoSuchElementException


class Testcnbloglink(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        '''初始化浏览器驱动和访问地址'''
        cls.driver = webdriver.Chrome()
        cls.base_url = 'https://www.cnblogs.com/'
        cls.driver.get(cls.base_url)
        cls.driver.maximize_window()  # 将浏览器设为全屏

    def test_1clickcateitem(self):
        '''测试点击一级菜单链接'''
        try:
            cate_parentid = '//*[@id="cate_item"]/li'  # 所有一级菜单元素
            for i in range(1, 30):  # 循环一级菜单元素下标
                cate_id = cate_parentid + '[' + str(i) + ']'  # 得到的一级菜单元素
                # 循环点击每一个一级菜单
                menu = self.driver.find_element_by_xpath(cate_id)
                menu.click()
                menu = self.driver.find_element_by_xpath(cate_id)  # 点击菜单之后,页面刷新,需要重新赋值
                # 断言,判断跳转链接是否正确
                if menu.get_attribute('id')[10:] == '0':
                    self.assertEqual(self.driver.current_url, 'https://www.cnblogs.com/cate/all/')
                elif menu.get_attribute('id')[10:] == '-1':
                    self.assertEqual(self.driver.current_url, 'https://www.cnblogs.com/comment/')
                else:
                    self.assertEqual(self.driver.current_url, 'https://www.cnblogs.com/cate/' +
                                     menu.get_attribute('id')[10:] + '/')
        except NoSuchElementException:
            print('没有找到一级菜单,一级菜单点击完成')

    def test_2clickcatecontent(self):
        '''点击第二级菜单'''
        try:
            cate_parentid = '//*[@id="cate_item"]/li'  # 一级菜单的所有元素
            for i in range(1, 30):
                cate_id = cate_parentid + '[' + str(i) + ']'
                above = self.driver.find_element_by_xpath(cate_id)  # 一级菜单元素
                aid = above.get_attribute("id")[10:]   # 一级菜单的id值
                try:
                    cate_content_parentid = '// *[ @ id = "cate_content_block_' + aid + '"]'  # 二级菜单的所有元素
                    for f in range(1, 30):
                        # 鼠标悬停一级菜单
                        ActionChains(self.driver).move_to_element(above).perform()
                        cate_content_id = cate_content_parentid+'/div[2]'+'/ul'+'/li['+str(f)+']'+'/a'  # 二级菜单元素
                        menu2 = self.driver.find_element_by_xpath(cate_content_id)
                        # 拖动元素到可见区域--scrollIntoView() 拉到顶部显示,有可能会被导航栏遮挡,定位不到而报错;
                        # scrollIntoView(false)可视区域底部对齐
                        if f == 17:
                            self.driver.execute_script("arguments[0].scrollIntoView(false);", menu2)
                        # 循环点击二级菜单
                        menu2.click()
                        # 点击二级菜单之后,页面刷新,需要对一级菜单重新赋值,要不然找不到同一个一级菜单下的其他二级菜单
                        above = self.driver.find_element_by_xpath(cate_id)
                except NoSuchElementException:
                    print('没有找到二级菜单,二级菜单点击完')
        except NoSuchElementException:
            print('没有找到一级菜单,一级菜单悬停完成')

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

 

posted @ 2019-10-19 18:24  lengjf  阅读(1532)  评论(0编辑  收藏  举报