如何从Hoover菜单中打开WebElement Selenium JAVA

时间:2017-04-05 22:54:42

标签: java html selenium xpath css-selectors

你好我是使用selenium的新手,我试图从网页上执行一些测试。

这是我的代码:

 System.setProperty("webdriver.gecko.driver","C:\\DRIVERS\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    //Open Portal Fiscal
    driver.get("http://150.23.110.111/Retenciones/");
    //Find what field and enter the user and password
    driver.findElement(By.id("frmLogin:txtUsr")).sendKeys("arrubio");
    driver.findElement(By.id("frmLogin:txtPwd")).sendKeys("gnp00gnp");
    driver.findElement(By.id("frmLogin:butLogin")).click();
    Actions action = new Actions(driver);
    WebElement we = driver.findElement(By.xpath(""));
    action.moveToElement(we).moveToElement(driver.findElement(By.xpath("")));

我可以毫无问题地进入页面,我可以输入用户和密码进行登录,但是下一页上有一个我无法使用的胡佛菜单并停止我的自动执行。

这是xpath和csspath:

xpath:/ html / body / div [3] / div / div / form / div / ul / li [1] / ul / li [1] / a / span

csspath:html body div#content div#leftPanel.ui-layout-unit.ui-widget.ui-widget-content.ui-corner-all.ui-layout-west.blankBck div.ui-layout-unit -content.ui-widget-content表单#j_id1833690111_27e067e8.blankBck div#j_id1833690111_27e067e8:j_id1833690111_27e0678e.ui-menu.ui-menubar.ui-widget.ui-widget-content.ui-corner-all.ui-helper-clearfix ul。 ui-menu-list.ui-helper-reset li.ui-widget.ui-menuitem.ui-corner-all.ui-menu-parent.ui-menuitem-active ul.ui-widget-content.ui-menu- list.ui-corner-all.ui-helper-clearfix.ui-menu-child.ui-shadow li.ui-menuitem.ui-widget.ui-corner-all a.ui-menuitem-link.ui-corner-所有span.ui-menuitem-text

这是检查“Búsqueda”按钮的元素。

<ul class="ui-widget-content ui-menu-list ui-corner-all ui-helper-clearfix ui-menu-child ui-shadow" role="menu" style="display: block; height: auto; z-index: 1013; left: 0px; top: 28px;">
<li class="ui-menuitem ui-widget ui-corner-all" role="menuitem">
<a class="ui-menuitem-link ui-corner-all" href="/Retenciones/main/faces/m_evaPuntual.xhtml" style="width:120px" tabindex="-1">
<span class="ui-menuitem-text">Búsqueda</span>
</a>
</li>
<li class="ui-menuitem ui-widget ui-corner-all" role="menuitem">
</ul>

如何从胡佛菜单中选择并打开“Búsqueda”按钮?

感谢您的关注:)

2 个答案:

答案 0 :(得分:2)

尝试使用:

Actions action = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("xpath for menu"));
WebElement item = driver.findElement(by.cssSelector("css selector values for Búsqueda"));
action.moveToElement(menu).moveToElement(item ).click().build().perform();

答案 1 :(得分:1)

使用action class

尝试以下代码
WebElement menu_element = driver.findElement(By.xpath("your_menu_xpath"));

WebDriverWait wait = new WebDriverWait(driver, 10);   //Explicit wait method, wait for web-element till 10 seconds so, your driver should able to find the web-element.
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("Your_submemu_xpath"))));

WebElement sub_menu_element = driver.findElement(By.xpath("Your_submemu_xpath"));
Actions action = new Actions(driver);
action.moveToElement(menu_element).moveToElement(sub_menu_element).click().build().perform();

说明:

1)首先找到menu element

2)提供explicit wait方法几秒钟,以便您的驱动程序可以找到您想要使用的sub_menu_element

3)在明确等待之后找到你想要的sub_menu element

4)使用Action class尝试从menu to sub menu移动元素。