有人可以帮助显示为什么这不起作用吗?
页面对象:
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()
时,单击元素并显示我想要的下拉列表,因此选择器正确。 最佳答案
您需要在perform()
类方法上调用Actions
public void hover_Product_Category(){
action.moveToElement(product_Category).perform();
}