【问题标题】:Not able to click on menu option from selenium无法从硒单击菜单选项
【发布时间】:2012-11-19 15:08:35
【问题描述】:

我正在尝试为我的 WebApp 选择菜单选项,能够单击菜单,菜单选项也可见但无法单击菜单选项。

如果我尝试从 Selenium API 检查菜单选项的可见性,它说不可见,但实际上它已经在屏幕上可见。

是否有使用 Selenium IE Driver 选择菜单和菜单选项的标准。

以下是菜单/菜单选项代码:

<DIV style="BORDER-BOTTOM: 2px outset; POSITION: absolute; BORDER-LEFT: 2px outset; WIDTH: 162px; FONT-FAMILY: Tahoma; BACKGROUND: #e4e4e4; HEIGHT: 106px; FONT-SIZE: 8pt; BORDER-TOP: 2px outset; TOP: 0px; BORDER-RIGHT: 2px outset; LEFT: 0px">
<DIV style="PADDING-BOTTOM: 2px; PADDING-LEFT: 15px; PADDING-RIGHT: 2px; HEIGHT: 17px; COLOR: black; CURSOR: hand; PADDING-TOP: 2px" onmouseover="this.style.background='darkblue';this.style.color='white';" onmouseout="this.style.background=this.runtimeStyle.background;this.style.color='black';" onclick="parent.smConfig_Click(0,'smConfig0')"><SPAN>Config. Management</SPAN> </DIV><INPUT id=lgx_smConfig01 type=hidden SP="" LP="<LinkParams lgx_ID='WideDetail'  FromDate='@Data.DefaultDate.FromDate~' ToDate='@Data.DefaultDate.ToDate~' lgx_DetailID='dtlConfig' FirstTime='1' ConfigType='2'  />" Modal="" Features="" Target="_top" Action="lgx_Frame.aspx"> 
<DIV style="PADDING-BOTTOM: 2px; PADDING-LEFT: 15px; PADDING-RIGHT: 2px; HEIGHT: 17px; COLOR: black; CURSOR: hand; PADDING-TOP: 2px" onmouseover="this.style.background='darkblue';this.style.color='white';" onmouseout="this.style.background=this.runtimeStyle.background;this.style.color='black';" onclick="parent.smConfig_Click(1,'smConfig1')"><SPAN>Custom Configuration</SPAN> </DIV><INPUT id=lgx_smConfig11 type=hidden SP="" LP="<LinkParams lgx_ID='Default'  lgx_ListID='lstCustomConfig' ConfigType='1'  />" Modal="" Features="" Target="_top" Action="lgx_Frame.aspx"> 
<DIV style="PADDING-BOTTOM: 2px; PADDING-LEFT: 15px; PADDING-RIGHT: 2px; HEIGHT: 17px; COLOR: black; CURSOR: hand; PADDING-TOP: 2px" onmouseover="this.style.background='darkblue';this.style.color='white';" onmouseout="this.style.background=this.runtimeStyle.background;this.style.color='black';" onclick="parent.smConfig_Click(2,'smConfig2')"><SPAN>Custom Transition</SPAN> </DIV><INPUT id=lgx_smConfig21 type=hidden SP="" LP="<LinkParams lgx_ID='Default'  lgx_ListID='lstCustomTransition' Flag='512' Title='Custom'  />" Modal="" Features="" Target="_top" Action="lgx_Frame.aspx"> 
<DIV style="PADDING-BOTTOM: 2px; PADDING-LEFT: 15px; PADDING-RIGHT: 2px; HEIGHT: 17px; COLOR: black; CURSOR: hand; PADDING-TOP: 2px" onmouseover="this.style.background='darkblue';this.style.color='white';" onmouseout="this.style.background=this.runtimeStyle.background;this.style.color='black';" onclick="parent.smConfig_Click(3,'smConfig3')"><SPAN>Person Hierarchy Level</SPAN> </DIV><INPUT id=lgx_smConfig31 type=hidden SP="" LP="<LinkParams lgx_ID='Default'  lgx_ListID='lstPersonLevel'  />" Modal="" Features="" Target="_top" Action="lgx_Frame.aspx"> 
<DIV style="PADDING-BOTTOM: 2px; PADDING-LEFT: 15px; PADDING-RIGHT: 2px; HEIGHT: 17px; COLOR: black; CURSOR: hand; PADDING-TOP: 2px" onmouseover="this.style.background='darkblue';this.style.color='white';" onmouseout="this.style.background=this.runtimeStyle.background;this.style.color='black';" onclick="parent.smConfig_Click(4,'smConfig4')"><SPAN>Role</SPAN> </DIV><INPUT id=lgx_smConfig41 type=hidden SP="" LP="<LinkParams lgx_ID='Default'  lgx_ListID='lstRoleConfig'  />" Modal="" Features="" Target="_top" Action="lgx_Frame.aspx"> 
<DIV style="PADDING-BOTTOM: 2px; PADDING-LEFT: 15px; PADDING-RIGHT: 2px; HEIGHT: 17px; COLOR: black; CURSOR: hand; PADDING-TOP: 2px" onmouseover="this.style.background='darkblue';this.style.color='white';" onmouseout="this.style.background=this.runtimeStyle.background;this.style.color='black';" onclick="parent.smConfig_Click(5,'smConfig5')"><SPAN>Status</SPAN> </DIV><INPUT id=lgx_smConfig51 type=hidden SP="" LP="<LinkParams lgx_ID='Default'  lgx_ListID='lstStatusTy'  />" Modal="" Features="" Target="_top" Action="lgx_Frame.aspx"> </DIV></DIV>

在上面的菜单选项代码中,我需要点击Role选项。 此选项在单击 configure 菜单时可见,但单击任何此类菜单选项都不会发生,它也不会引发任何异常。

请让我知道您的 cmets 或解决此问题的建议。

谢谢。

试过代码1:

WebElement configMenu = driver.findElement(By.id("mConfigLabel"));
configMenu.click();
WebElement menuOption = driver.findElement(By.xpath("//div[@id='oMenuCntxtsmConfig']/div/div[4]"));
menuOption.click();

试过代码2:

WebElement configMenu = driver.findElement(By.id("mConfigLabel"));
configMenu.click();
WebElement menuOption = driver.findElement(By.xpath("//div/descendant:span[contains(text(),'Role']"));
menuOption.click();

尝试过代码 3:

configMenu.click();
Actions builder = new Actions(driver);
WebElement roleMenuOption = driver.findElement(By.xpath("//div[@id='oMenuCntxtsmConfig']/div/div[4]"));
builder.moveToElement(configMenu).click(roleMenuOption).build().perform();

尝试过代码 4:

WebElement configMenu = driver.findElement(By.id("mConfigLabel"));
configMenu.click();
WebElement roleMenuOption = driver.findElement(By.xpath("//div[@id='oMenuCntxtsmConfig']/div/div[4]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", roleMenuOption);

【问题讨论】:

  • 你在尝试什么代码?你能把它贴在这里吗?
  • @Abhi_Mishra 感谢 Abhi 的回复,我已经放了四个示例代码,我尝试过但没有解决问题。

标签: internet-explorer selenium ui-automation selenium-webdriver


【解决方案1】:

根据我对您问题的理解,您只是点击了部门。

您的 html 代码表明 onclick 事件与 div 相关联。 此 onClick 事件可能使您的输入(最初是隐藏的)可见。

现在您也需要点击此输入

例如,对于单击角色选项,您需要附加此行

driver.findElementByID("lgx_smConfig41").click();

希望对您有所帮助。

【讨论】:

  • 感谢您的回复,我试试,我会尽快给您更新。
  • 我试过了,但它抛出了 ElementNotVisibleException,所以我尝试使用 JavascriptExecutor : ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);但没有运气:(
【解决方案2】:

使用 selenium 玩 Dropdown/menu 很棘手,因为下拉/菜单项是隐藏的,并且在用户单击 Dropdown/Menu 之前不可见。

你的方法有点对也有点错..

正确您访问下拉菜单/菜单的方式

错误您访问下拉菜单/菜单中的项目的方式,因为它们隐藏在菜单/下拉列表中无法直接访问。

请参阅下面的代码以正确访问特定的菜单项,

WebElement DropdownList = driver.findElement(By.id("mConfigLabel"));
Select clickThis = new Select(DropdownList);
clickThis.selectByValue("Role");

我假设你的 men/Dropdown 的 ID 是“mConfigLabel”

stackover 流中有不同的代码风格用于访问菜单项...请看一下。

希望这会有所帮助....一切顺利:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多