2017-02-09 56 views
0

有人可以幫助顯示爲什麼這不起作用嗎?moveToElement()執行懸停動作

頁面對象:

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.interactions.Actions; 
import org.openqa.selenium.support.CacheLookup; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.How; 
import org.openqa.selenium.support.PageFactory; 

    public class NavBarPO { 

    WebDriver driver; 
    Actions action; 

    public NavBarPO(WebDriver driver){ 
     this.driver = driver; 
     PageFactory.initElements(driver, this); 
     action = new Actions(driver); 
    } 
     @CacheLookup 
     @FindBy(how = How.CSS, using = "li.menu-item.menu-item-type-taxonomy.menu-item-object-wpsc_product_category.menu-item-has-children.has_children > a") 
     private WebElement product_Category;  

     public void hover_Product_Category(){ 
      action.moveToElement(product_Category); 
     }  
} 

測試:

public class OpenDemos { 

     @BeforeTest 
     public void Initialize() { 
      System.setProperty("webdriver.chrome.driver", "C:/Users/u6028938/Documents/Selenium Java/chromedriver.exe"); 
      System.setProperty("webdriver.gecko.driver", "C:/Users/u6028938/Documents/Selenium Java/geckodriver.exe"); 
     } 

     @Test 
      public void SecondTest() throws InterruptedException { 
       WebDriver driver = new FirefoxDriver(); 
       NavBarPO nav = new NavBarPO(driver); 
       driver.get("http://www.store.demoqa.com"); 
       Thread.sleep(3000); 
       nav.Hover_Product_Category(); 
       System.out.println("Successfully Executed Test!"); 
       Thread.sleep(10000); 
       driver.quit(); 
      } 
    } 

nav.hover_Product_Category()根本什麼都不做,甚至不是一個錯誤。當我使用.click()而不是.moveToElement()單擊該元素並顯示我想要的下拉列表,所以選擇器是正確的。

回答

1

你需要調用perform()Actions類的方法

public void hover_Product_Category(){ 
    action.moveToElement(product_Category).perform(); 
} 
+0

我包含'.perform()',現在測試引發異常'org.openqa.selenium.UnsupportedCommandException:POST/session/d33e2dc4-2b3b-4f1b-a22d-20673c2445b7/moveto與已知命令不匹配 –

+0

@GabrielAbel異常發生在哪裏? on'action.moveToElement(product_Category).perform();'? – Guy

+0

正是@Guy。當我包含'.perform()'。當它只是'action.moveToElement(product_Category);'這個異常沒有發生。 –

0

.moveToElement()FirefoxDriver正常工作。解決方法是將測試更改爲ChromeDriver。如果需要測試Firefox,則可能會在某些情況下使用.click()替換 .moveToElement()