Java 我正在使用Actions类移动到某个元素,但无法单击下拉列表中的元素 网站: 鼠标悬停在元素上:登录帐户和积分 下拉元素:“免费加入” 我正在使用Firefox浏览器

Java 我正在使用Actions类移动到某个元素,但无法单击下拉列表中的元素 网站: 鼠标悬停在元素上:登录帐户和积分 下拉元素:“免费加入” 我正在使用Firefox浏览器,java,selenium-webdriver,action,Java,Selenium Webdriver,Action,您不应该使用Thread.sleep(),因为这是一种不好的做法。改用WebDriverWait 从您的代码来看,您的悬停看起来是正确的,但您需要短暂的等待,以确保面板打开,“免费加入”按钮可见且可单击。一个WebDriverWait可以很容易地解决这个问题。。。您只需等待按钮可单击,然后单击它 public static void main() throws InterruptedException { Thread.sleep(5000); Actions a

您不应该使用
Thread.sleep()
,因为这是一种不好的做法。改用
WebDriverWait

从您的代码来看,您的悬停看起来是正确的,但您需要短暂的等待,以确保面板打开,“免费加入”按钮可见且可单击。一个
WebDriverWait
可以很容易地解决这个问题。。。您只需等待按钮可单击,然后单击它

public static void main() throws InterruptedException {
        Thread.sleep(5000);
        Actions a = new Actions(driver);
        WebElement as = driver.findElement(By.xpath(".//*[@id='yourAccount']"));
        a.moveToElement(as).build().perform();
        WebElement login = driver.findElement(By.xpath("//ul[@class='hFlyout guest gnf_nav_depth2_list']//li[12]//button"));
        System.out.println(login.isDisplayed());
        login.click();
        Thread.sleep(5000);
        driver.switchTo().frame(1);
        System.out.println("pass");
        driver.findElement(By.linkText("Join for free")).click();
        Thread.sleep(5000);
}
这个代码对我有用

driver.get("https://www.sears.com/");
Actions hover = new Actions(driver);
hover.moveToElement(driver.findElement(By.id("yourAccount"))).build().perform();
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-action='join']"))).click();