簡體   English   中英

如何從Hoover菜單Selenium JAVA打開WebElement

[英]How to open a WebElement from a Hoover Menu Selenium JAVA

您好,我是使用硒的新手,我正嘗試從網頁上執行一些測試。

這是我的代碼:

 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正文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-clear -list.ui-helper重置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-a.ui-menuitem-link.ui-corner-所有跨度。用戶界面文本

這是檢查“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”按鈕?

感謝您的關注:)

嘗試使用:

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

在下面的代碼中使用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移動menu to sub menu

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM