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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM