[英](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中获取密码,请执行以下操作:
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_MenuItemVerticalSelected > a
Might become 可能成为
<div> // <- Parent div HTML
div > ul > li:nth-child(2) > ul > li.igdm_MenuItemVertical.igdm_MenuItemVerticalParent.igdm_MenuItemVerticalSelected > a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.