鼠标悬停后单击

时间:2015-02-05 10:40:20

标签: java selenium-webdriver

亲爱的,我在这段代码中需要一些帮助

首先:我需要让测试用例在

时通过if语句
driver.findElement(By.id("login:popupPanel-tr")).isDisplayed());
显示

id以运行条件,如果没有if条件

则完成测试用例

第二:我需要点击元素

driver.findElement(By.id("home:regNew")).click();

当我执行此操作鼠标时,现在它没有单击并且测试用例失败

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import java.sql.*;

driver.findElement(By.id("login:username")).clear();
driver.findElement(By.id("login:username")).sendKeys("sarasf");
driver.findElement(By.id("login:password")).clear();
driver.findElement(By.id("login:password")).sendKeys("P@ssw0rd");
driver.findElement(By.id("login:login")).click();

if (driver.findElement(By.id("login:popupPanel-tr")).isDisplayed()) {
    driver.findElement(By.id("login:j_id54")).click();
    Thread.sleep(3000);
    driver.findElement(By.id("login:username")).clear();
    driver.findElement(By.id("login:username")).sendKeys("abasiouny_shop");
    Thread.sleep(1000);
    driver.findElement(By.id("login:password")).clear();
    driver.findElement(By.id("login:password")).sendKeys("passw0rd");
    Thread.sleep(1000);
    driver.findElement(By.id("login:login")).click();
    Thread.sleep(30000);
    Actions action = new Actions(driver);
    WebElement subElement =  driver.findElement(By.cssSelector("p.service"));
    action.moveToElement(subElement);
    action.click();
    action.perform();
    Thread.sleep(2000);   
    assertEquals("جديد",    driver.findElement(By.id("home:regNew")).getText());
}else{
    Thread.sleep(30000);
    Thread.sleep(2000);
    Actions action = new Actions(driver);
    WebElement element = driver.findElement(By.cssSelector("p.service"));   action.moveToElement(element).moveToElement(driver.findElement(By.cssSelector    ("p.service"))).build().perform();
    Thread.sleep(2000);
    driver.findElement(By.id("home:regNew")).click();
}

1 个答案:

答案 0 :(得分:0)

1)是什么阻止你这样做?

if(driver.findElement(By.id("login:popupPanel-tr")).isDisplayed())
{//condition
}

2)你是说在鼠标悬停后出现一个你需要点击的元素吗?如果是这种情况,则需要使用Actions类mouseOver API,然后执行click元素。 如果这有帮助,请告诉我。