moveToElement()执行悬停操作

时间:2017-02-09 11:36:04

标签: java selenium selenium-webdriver

有人可以帮助说明为什么这不起作用吗?

页面对象:

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()时,单击该元素并显示我想要的下拉列表,因此选择器是正确的。

2 个答案:

答案 0 :(得分:3)

您需要在 UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom]; useButton.frame = CGRectMake(0, 0, 20,17); useButton.layer.masksToBounds = NO; useButton.layer.shadowOffset = CGSizeMake(3, 3); useButton.layer.shadowRadius = 3; useButton.layer.shadowOpacity = 0.1; useButton.layer.shadowColor = [UIColor blackColor].CGColor; useButton.backgroundColor = [UIColor clearColor]; useButton.tintColor = [UIColor whiteColor]; [useButton setImage:[UIImage imageNamed:@"image if any"] forState:0]; [useButton addTarget:self action:@selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside]; [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:useButton]]; 课程方法

上致电perform()
Actions

答案 1 :(得分:0)

moveToElement中的函数FirefoxDriver无效。解决方案是将测试更改为ChromeDriver。如果您需要测试Firefox,可以将moveToElement替换为click