使用selenium

时间:2017-08-15 04:22:54

标签: c# python .net selenium xpath

我想选择"应用程序处理"将鼠标悬停在" Asmt Admin"之后的菜单项父菜单项选项。 html如下:

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

我尝试如下:

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

但&#34;应用程序处理&#34;菜单项没有被点击,命令行也没有显示任何错误。

enter image description here

我做错了什么?请帮忙。

2 个答案:

答案 0 :(得分:0)

您的代码对我来说非常好,但我会建议如下2个更改:

  1. 当你正在做browser.get(('localhost:81'))并接下来做(By.XPATH, "//a/span[text()='Asmt Admin']")时,我认为你可以省略对WebDriverWait的引用。
  2. 搜索元素(By.XPATH, "//a/span[text()='Application Processing']")而不是EC.visibility_of_element_located时,您可以使用EC.element_to_be_clickable
  3. 您的最终代码如下:

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

答案 1 :(得分:0)

我能够使用.Net和C#修复此问题。我们应该能够在Python中做类似的事情。我相信,重要的部分是使用xpath选择正确的锚标签,使用回车键来点击锚点按钮。

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);
        }
    }
}