2017-02-09 60 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()