鼠标悬停功能仅在第一时间有效

时间:2019-05-17 04:01:11

标签: python-3.x selenium mouseover

我正在用Python / Selenium编写一些测试,并试图验证Dropdown菜单系统。当您“鼠标悬停”在顶部菜单项之一时,它将显示其下方的子菜单项,从而允许测试代码单击子菜单。

我遇到的问题是,我可以将鼠标悬停在顶部菜单项上,并且它可以正确打开子菜单,但是如果我尝试打开同一顶部菜单,以便可以选择下一个子菜单项,则“ hover_over_menu_item”功能什么也不做,没有错误,但不会打开顶部菜单项。

但是,如果我先将鼠标悬停在另一个“顶部”菜单项上,以便它显示该子菜单,则可以将鼠标悬停在原始“顶部菜单”上,并且可以正确打开。

HTML:

   # The HTML looks like:         
   <div class="desktop-navbar">
       <dropdown class="nav-bar-item ">  <---First top level menu item
           <label class=”animate” for=”0menu"
           <ul class=”animate” for=”0menu”> <--- List of sub menu's
       <dropdown class="nav-bar-item ">  <---Second top level menu item
       <dropdown class="nav-bar-item ">  <---Third top level menu item

代码:

    def open_top_menu(top_menu_name)    
        # Retrieve an array of the Web Elements that make up the top menu
        css = '.nav-bar-item'
        we_menu_array = driver.find_elements(By.CSS_SELECTOR, css)

        # I then scan through this array looking for the Menu name
        # I require and after I have found the correct top menu
        # item, I mouse over it to expand the sub-menu:
        count = len(we_menu_array)
        for i in range(0, count):
            menu_name = we_menu_array[i].text.strip()
            if menu_name.upper() == top_menu_name.upper():
                 hover_over_menu_item(we_array[i])
                 result = True
                break

        return result


def hover_over_menu_item(we):
    action = ActionChains(driver)
    action.move_to_element(we)
    action.perform()

0 个答案:

没有答案