【问题标题】:How to open a WebElement from a Hoover Menu Selenium JAVA如何从 Hoover 菜单 Selenium JAVA 打开 WebElement
【发布时间】:2017-04-05 22:54:41
【问题描述】:

您好,我是使用 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 form#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 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”按钮?

感谢关注:)

【问题讨论】:

    标签: java html selenium xpath css-selectors


    【解决方案1】:

    尝试使用:

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

    【讨论】:

      【解决方案2】:

      使用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 移动元素。

      【讨论】:

      • 你试过这个代码吗?我对代码的解释对你有帮助吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多