繁体   English   中英

(Selenium WebDriver)菜单单击通过,但页面上没有任何反应

[英](Selenium WebDriver) Menu click pass but nothing happens on page

我正在为旧的“基础设施”(我对此不太了解)页面编写硒C#测试代码。 在元素引用方面,该页面没有给我太多选择。 像下面的HTML一样,我想单击INVENTORY,它是菜单项的子菜单,但是没有元素ID或Name,所以我使用了XPath(请参见下面的代码)。 看起来webdriver可以识别xpath,但是当click函数执行并完成时,页面上什么也没有发生。 它不会打开预期的页面。 我在这里做错什么了吗?

 <li class="igdm_MenuItemVertical igdm_MenuItemVerticalParent " unselectable="on" data-ig="x:1171509256.11:adr:1.2" adr="1.2"><A onclick="{return false;}" tabIndex=-1 class="igdm_MenuItemVerticalLink " href="#/Inventory..."> <a onclick="{return false;}" tabIndex=-1 class="igdm_MenuItemVerticalLink> <img class="igdm_MenuItemVerticalIcon " alt=" Inventory..." src="../Images/report16.gif"> <span tabIndex=-1 unselectable="on"> INVENTORY...</span> </a> 

码:

private By FileSubMenu = By.XPath(".//li/a/span[text()=' Inventory...']");

    public HomePage SubMenu()
    {
        int retryCount = 0;
        while (true && retryCount < Constants.RETRY_COUNT)
        {
            try
            {
                Thread.Sleep(3000);
                IWebElement element = _driver.FindElement(FileSubMenu);
                Actions Rmouseover = new Actions(_driver);
                Rmouseover.MoveToElement(element).Click().Perform();
                return this;
            }
            catch (Exception ex) when (ex is WebDriverTimeoutException || ex is TimeoutException)
            {
                retryCount++;
                Thread.Sleep(3000);
            }
        }
        return this;
    }

XPath可以,但是我更喜欢CSS Selector,因为它倾向于产生更少的脆性选择器(虽然不是基于DOM的,但是仍然可以)。 要在Chrome中获取密码,请执行以下操作:

  1. 右键单击元素,然后选择“检查”
  2. 右键单击DevTools的DOM资源管理器中的元素
  3. 选择“复制”>“复制选择器”

因此,您的代码将如下所示:

IWebElement element =  driver.FindElement(By.CssSelector("paste_the_selector_here"));
// Click via Selenium's Click method
element.Click();
// Or, click via JavaScript
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", element);

另外,与其单击<span>还可能要单击<a>因为它具有onclick事件侦听器。

如果选择器包含一个动态父级的ID,那么您将要删除它。 要删除它,请在DevTool的“元素”选项卡中找到具有该ID的元素。 双击id="..."部分并删除整个内容。 现在获取子元素的选择器。

这个例子

<div id="ctl00_NavBarAdmin_WebDataMenu1"> // <- Parent div html
#ctl00_NavBarAdmin_WebDataMenu1 > ul > li:nth-child(2) > ul > li.igdm_MenuItemVertical.igdm_MenuItemVerticalParent.igdm_Me‌​nuItemVerticalSelect‌​ed > a

可能成为

<div> // <- Parent div HTML
div > ul > li:nth-child(2) > ul > li.igdm_MenuItemVertical.igdm_MenuItemVerticalParent.igdm_Me‌​nuItemVerticalSelect‌​ed > a 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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