Java 鼠标悬停后单击

Java 鼠标悬停后单击,java,selenium-webdriver,Java,Selenium Webdriver,亲爱的我需要一些帮助在这个代码 首先:我需要让测试用例在 driver.findElement(By.id("login:popupPanel-tr")).isDisplayed()); id显示以运行条件,如果没有,则在没有条件的情况下完成测试用例 第二步:我需要单击元素 driver.findElement(By.id("home:regNew")).click(); 当我现在这样做的时候,鼠标移动到上面之后,它没有点击,测试用例失败了 import java.util.regex.Pa

亲爱的我需要一些帮助在这个代码

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

driver.findElement(By.id("login:popupPanel-tr")).isDisplayed());
id
显示以运行条件,如果没有,则在没有条件的情况下完成测试用例

第二步:我需要单击元素

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) 是什么阻止你这么做的

if(driver.findElement(By.id("login:popupPanel-tr")).isDisplayed())
{//condition
}
2) 你是说鼠标悬停后会出现一个元素,你需要点击它吗?如果是这种情况,那么您需要使用Actions类mouseOver API,然后执行click-on元素。
如果有帮助,请告诉我。

为什么使用太多的线程。sleep()?它不是测试性能的最佳实践和杀手
Selenium
提供了
显式
隐式
等待来处理此类情况。需要等待页面加载使用
Driver.Manage().timeout().SetPageLoadTimeout(某物)
1)当我执行代码中提到的if条件时,如果浏览器发现此弹出窗口,它会正常工作,但当它未显示时,测试用例失败,我的意思是它不会转到其他{}2)这正是我需要的,因此我感谢您的帮助。现在我明白了。。您必须执行Try/Catch或ExpectedConditions.elementtobelickable(By)。在预期条件中有很多变体,您可以选择任何一种适合您的外部等待(WebDriverWait)。你的第二个问题进展如何?你可以在哪里浏览Actions classNo仍然是同一个问题,它找到元素“p.service”并将鼠标移到上面,但它不能单击它下面出现的元素。为什么你不能直接执行moveToElement,然后使用click?它也不起作用。它只高亮显示元素,但不单击它