简体   繁体   English

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

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

I am writing a selenium C# test code for an old "infragistics" (I seriouly don't know too much about this) page. 我正在为旧的“基础设施”(我对此不太了解)页面编写硒C#测试代码。 The page doesn't give me that much options when it comes to elements references. 在元素引用方面,该页面没有给我太多选择。 Like in the HTML below I want to click on INVENTORY which is a submenu of a menu item but there is no element Id or Name so I used XPath (see code below). 像下面的HTML一样,我想单击INVENTORY,它是菜单项的子菜单,但是没有元素ID或Name,所以我使用了XPath(请参见下面的代码)。 It looks like webdriver recognizes that xpath but when the click function executes and complete, nothing happens on the page. 看起来webdriver可以识别xpath,但是当click函数执行并完成时,页面上什么也没有发生。 It does not open the expected page. 它不会打开预期的页面。 Is there something that I am doing wrong here? 我在这里做错什么了吗?

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

Code: 码:

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 is okay, but I prefer CSS Selector as it tends to yeild less brittle selectors (not so location in the DOM based, but still can be). XPath可以,但是我更喜欢CSS Selector,因为它倾向于产生更少的脆性选择器(虽然不是基于DOM的,但是仍然可以)。 To get one in Chrome: 要在Chrome中获取密码,请执行以下操作:

  1. Right-click on the element and select 'Inspect' 右键单击元素,然后选择“检查”
  2. Right-click on the element in the DOM explorer of DevTools 右键单击DevTools的DOM资源管理器中的元素
  3. Select "Copy" > "Copy selector" 选择“复制”>“复制选择器”

So then your code would look something like: 因此,您的代码将如下所示:

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

Also, instead of clicking the <span> you may want to click on the <a> instead as it has an onclick event listener. 另外,与其单击<span>还可能要单击<a>因为它具有onclick事件侦听器。

If the selector includes an ID for a parent that is dynamic, then you'll want to remove it. 如果选择器包含一个动态父级的ID,那么您将要删除它。 To remove it, find the element with that ID in the DevTool's Element tab. 要删除它,请在DevTool的“元素”选项卡中找到具有该ID的元素。 Double-click on the id="..." part and delete the whole thing. 双击id="..."部分并删除整个内容。 Now get the selector for the child element. 现在获取子元素的选择器。

This example 这个例子

<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

Might become 可能成为

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

相关问题 单击按钮没有任何反应 - Nothing happens on button click 使用Webdriver上传文件:sendKeys()之后没有任何反应 - Upload file with Webdriver: nothing happens after sendKeys() Selenium Chrome驱动程序:单击功能的Javascript。 元素不可见或没有任何反应 - Selenium Chrome Driver: Javascript on click function. element not visible or nothing happens 使用Selenium WebDriver java单击动态加载页面的元素 - Click on element of a dynamic loaded page using Selenium WebDriver java 我将XMLHttpRequest用于菜单,当我请求其中包含javascript的页面HTML时,什么也没发生,为什么? - I use XMLHttpRequest for a menu and when i request a page HTML that contains javascript inside it nothing happens,why? 通过Selenium WebDriver访问JavaScript菜单 - Accessing javascript menu by selenium webDriver 单击表单html按钮时没有任何反应 - nothing happens when click on a form html button 可调用 Firebase Function 按钮单击没有任何反应 - Callable Firebase Function Nothing Happens On Button Click 单击“上传”按钮时没有任何反应 - Nothing happens when I click on the “Upload” button 当我点击按钮时,没有任何反应 - When I click on button, nothing happens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM