简体   繁体   中英

Selecting a menu item after hovering over a parent menu item using selenium

I want to select "Application Processing" menu item after I hover over the "Asmt Admin" parent menu item option. The html is as follows:

<div id="topmenu">
    <div id="ctl00_topMenu1" class="RadMenu RadMenu_GovernBlue rmSized">
        <ul class="rmRootGroup rmShadows rmHorizontal">
            <li class="rmItem">
                <a class="rmLink rmRootLink" href="#">
                    <span class="rmText rmExpandDown">Asmt Admin</span>
                </a>
                <div class="rmSlide">
                    <ul class="rmVertical rmGroup rmLevel1">
                        <li class="rmItem ">
                            <a class="rmLink" href="#">
                                <span class="rmText">Application Processing</span>
                            </a>
                        </li>
                    </ul>
                </div>
            </li>        
        </ul>
    </div>
</div>

I tried as follows:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

browser = webdriver.Chrome()
browser.get(('localhost:81'))

wait = WebDriverWait(browser, 10)
AsmtAdmin = wait.until(EC.visibility_of_element_located((By.XPATH, "//a/span[text()='Asmt Admin']")))
ActionChains(browser).move_to_element(AsmtAdmin).perform()

ApplicationProcessing = wait.until(EC.visibility_of_element_located((By.XPATH, "//a/span[text()='Application Processing']")))
ActionChains(browser).move_to_element(ApplicationProcessing).click().perform()

but the "Application Processing" menu item does not get clicked neither does the command line show any errors.

在此处输入图片说明

What am I doing wrong? Please help.

Your code looks pretty good to me but I will suggest 2 changes as follows:

  1. As you are doing browser.get(('localhost:81')) and next doing (By.XPATH, "//a/span[text()='Asmt Admin']") I think you can omit the reference to WebDriverWait .
  2. When searching for the element (By.XPATH, "//a/span[text()='Application Processing']") instead of EC.visibility_of_element_located you can use EC.element_to_be_clickable
  3. Your final code will look like:

     AsmtAdmin = driver.find_element_by_xpath("//a/span[text()='Asmt Admin']") actions = ActionChains(browser) actions.move_to_element(AsmtAdmin).perform() ApplicationProcessing = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, "//a/span[text()='Application Processing']")) ) ApplicationProcessing.click() 

I was able to fix this issue using .Net and C#. We should be able to do something similar in Python. I believe, the important parts were using xpath for selecting the correct anchor tag and the enter key to click the anchor button.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System;

namespace AutomateQA
{
    class Program
    {
        static void Main(string[] args)
        {
            var usernameStr = "admin";
            var passwordStr = "admin";

            IWebDriver chromeDriver = new ChromeDriver();
            chromeDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
            chromeDriver.Url = "http://localhost:81";

            var txtUserName = chromeDriver.FindElement(By.Id("txtUserName"));
            txtUserName.SendKeys(usernameStr);

            var txtPassword = chromeDriver.FindElement(By.Id("txtPassword"));
            txtPassword.SendKeys(passwordStr);

            var btnLogin = chromeDriver.FindElement(By.Id("btnLogin"));
            btnLogin.Click();

            var asmtAdminElement = chromeDriver.FindElement(By.LinkText("Asmt Admin"));
            var actions = new Actions(chromeDriver);
            actions.MoveToElement(asmtAdminElement).Perform();

            var applicationProcessingSubElement = chromeDriver.FindElement(By.XPath("//a[contains(.,'Application Processing')]"));
            applicationProcessingSubElement.SendKeys(Keys.Enter);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM