Python + + Unittest click a hyperlink to write Selenium test case

Test function: Click the link to the previous menu and secondary menu links Home Park blog site classification.

Problems encountered:

  1. Click the cycle when the secondary menu, click on the first two menu under a previous menu page refreshes, and then positioning the second secondary menu under the same sub-menu level,

Will get an error: Can not find the second secondary menu, then the need for a menu to reposition assignment;

  2. When too many secondary menu at the menu level, without visible area of ​​the page, then it will error: element not interactable, then need to slide the scroll bar.

 

# Coding. 8 = UTF- 
from Selenium Import the webdriver
 Import the unittest
 from selenium.webdriver Import ActionChains
 from selenium.common.exceptions Import NoSuchElementException 


class Testcnbloglink (of unittest.TestCase): 
    @classmethod 
    DEF setUpClass (CLS):
         '' ' initialize browser driver and access address ' '' 
        cls.driver = webdriver.Chrome () 
        cls.base_url = ' https://www.cnblogs.com/ ' 
        cls.driver.get (cls.base_url) 
        cls.driver.maximize_window ()   # set your browser to full screen 

    DEF test_1clickcateitem (Self):
         '' ' test menu click on a link ' '' 
        the try : 
            cate_parentid = ' // * [@ the above mentioned id = "cate_item"] / li '   # all of a menu element 
            for I in Range (. 1, 30):   # circulating a menu element subscript 
                cate_id = cate_parentid + ' [ ' + STR (I) + ' ] '   # a menu element obtained 
                # cycles click on each menu level 
                the mENU = self.driver.find_element_by_xpath (cate_id)
                menu.click () 
                the MENU = self.driver.  find_element_by_xpath(cate_id)   '# After clicking the menu, refresh the page, you need to re-assignment 
                # asserted the right to determine whether hoplinks 
                IF menu.get_attribute ( ' the above mentioned 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/ )
                 the else :
                    self.assertEqual (self.driver.current_url, ' https://www.cnblogs.com/cate/ ' + 
                                     menu.get_attribute ( ' ID ' ) [10:] + ' / ' )
         the except NoSuchElementException:
             Print ( ' not found a menu, a complete menu click ' ) 

    DEF test_2clickcatecontent (Self):
         ' '' click on the second level menu '' ' 
        the try : 
            cate_parentid = ' // * [@ ID = "cate_item"] / Li for'   # All elements of a menu
             I in Range (. 1, 30 ): 
                cate_id = cate_parentid + ' [ ' + STR (I) + ' ] ' 
                above = self.driver.find_element_by_xpath (cate_id)   # a menu element 
                aid = above.get_attribute ( " ID " ) [10:]    # ID value of a menu of 
                the try : 
                    cate_content_parentid = ' // * [@ ID = "cate_content_block_ ' + + AID ' "] '   # All elements of secondary menu 
                    for f inRange (. 1, 30 ):
                         # Hover a menu 
                        ActionChains (self.driver) .move_to_element (above) .perform () 
                        cate_content_id = cate_content_parentid + ' / div [2] ' + ' / UL ' + ' / Li [ ' STR + (F) + ' ] ' + ' / A '   # two menu elements 
                        Menu2 = self.driver.find_element_by_xpath (cate_content_id)
                         drag elements to the visible region --scrollIntoView () shown at the top is pulled, there may be a navigation column blocking, but less than the positioning error; # #
                        Align bottom (false) viewable area scrollIntoView Causes 
                        IF F ==. 17 : 
                            self.driver.execute_script ( " arguments [0] .scrollIntoView (false); " , Menu2)
                         # cycles two click menu 
                        menu2.click ()
                         # click two after-level menu, refresh the page, need to re-assignment to a menu, or else find other secondary menu under one menu level 
                        above = self.driver.find_element_by_xpath (cate_id)
                 the except NoSuchElementException:
                     Print ( ' not found two level menu, two menus click End ' )
        the except NoSuchElementException:
             Print ( ' did not find a menu, a complete menu hover ' ) 

    @classmethod 
    DEF tearDownClass (CLS): 
        cls.driver.quit ()

 

Guess you like

Origin www.cnblogs.com/lengjf/p/11704717.html