使用selenium webdriver无法执行鼠标操作

时间:2016-02-08 11:58:56

标签: java selenium selenium-chromedriver

以下是代码

WebDriver dr= new ChromeDriver();
dr.get("http://obsessory.com/");
dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[1]/a")).click();
dr.findElement(By.id("email")).sendKeys("username@gmail.com");
dr.findElement(By.name("LoginForm[password]")).sendKeys("password");
dr.findElement(By.xpath(".//[@id='signIn']/div[2]/div[3]/div[3]/input")).click();
Actions action = new Actions(dr);
WebElement we =  dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/a/span"));
action.moveToElement(we).moveToElement(dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/ul/li[1]/a"))).click().build().perform();

我想点击我的帐户'或任何其他链接。 Kindle告诉我该怎么做

2 个答案:

答案 0 :(得分:0)

尝试

Actions action= new Actions(dr);
WebElement we =  dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/a/span"));
action.moveToElement(we).perform();
By locator = By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/ul/li[1]/a");
dr.click(locator);

答案 1 :(得分:0)

@kavya

请尝试此代码。我认为你无法在密码文本框中输入密码。

密码:

dr.findElement(By.xpath("(//input[@id='email'])[2]")).sendKeys("obsessory");

对于菜单:

WebElement we = dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/a"));
WebElement ve = dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/ul/li[1]/a"));

Actions act = new Actions(dr);
act.moveToElement(we).click(ve).perform();

希望这会起作用