Java 如何选择div内的元素->;ul->;硒中的锂

Java 如何选择div内的元素->;ul->;硒中的锂,java,html,css,eclipse,selenium,Java,Html,Css,Eclipse,Selenium,我有一个菜单,在鼠标悬停时显示一个列表,我想单击注销。 我已经写了一些代码,但无法得到想要的结果 以下是我的Java代码: public void Logout() throws Exception { WebElement profileDropdown = driver.findElement(By.className("profile-dropdown")); //profileDropdown.click(); //profileDropdown.fi

我有一个菜单,在鼠标悬停时显示一个列表,我想单击注销。 我已经写了一些代码,但无法得到想要的结果

以下是我的Java代码:

public void Logout() throws Exception {
      WebElement profileDropdown = driver.findElement(By.className("profile-dropdown"));
      //profileDropdown.click();
      //profileDropdown.findElement(By.id("lnkLogout")).click();
      //Select oSelect = new Select(driver.findElement(By.className("profile-dropdown")));

         //oSelect.selectByVisibleText("Log Out");
      //List<WebElement> li = profileDropdown.findElements(By.id("lnkLogout"));
      //li.get(0).click();//If there are only two such element, here 1 is index of 2nd element in list returned.

      List<WebElement> elems = driver.findElements(By.cssSelector("ul>li>a"));
      elems.get(5).click();
      //profileDropdown.findElement(By.xpath("(//a[contains(text(),'Log Out')])[2]")).click();

  }
public void Logout()引发异常{
WebElement profileDropdown=driver.findElement(By.className(“profile dropdown”);
//Profile下拉列表。单击();
//profileDropdown.findElement(By.id(“lnkLogout”))。单击();
//Select oSelect=newselect(driver.findElement(By.className(“概要文件下拉列表”));
//oSelect.selectByVisibleText(“注销”);
//List li=profileDropdown.findElements(By.id(“lnkLogout”);
//li.get(0).click();//如果只有两个这样的元素,这里1是返回的列表中第二个元素的索引。
列表元素=driver.findElements(由.cssSelector(“ul>li>a”);
elems.get(5).单击();
//profileDropdown.findElement(By.xpath(//a[contains(text(),'logout'))][2])。单击();
}
我尝试了很多方法,你可以看到注释的代码行。对我来说什么都不管用

这是我的HTML代码,我正在为其执行自动化

<div style="display: none;" class="profile-dropdown">
                <ul>
                    <li><a href="https://consumers.keenu.pk/index.php/profile/">My Profile <!--<label id="lblProfilePercentage">0</label>--></a></li>
                    <li><a href="https://consumers.keenu.pk/index.php/transactionhist/">Transaction History</a></li>
                    <li><a href="https://consumers.keenu.pk/index.php/customer-care/helpline">Helpline</a></li>
                    <li><a href="https://consumers.keenu.pk/index.php/pin-pass/">PIN &amp; Password</a></li>
                    <li><a href="https://consumers.keenu.pk/index.php/settings/">Favorites</a></li>
                    <li><a href="#" id="lnkLogout" style="cursor:pointer">Log Out</a></li>
                </ul>
            </div>

它能够找到“profile dropdown”元素,但随后抛出异常,无法找到list元素


请提供帮助。

如果您说当您在菜单上单击鼠标时,菜单项会出现,那么单击它将不起作用。您需要:

1.先将鼠标悬停在菜单上

2.需要等待,直到注销菜单项(链接)可见

3.点击它

Actions action = new Actions(driver);
action.moveToElement(profileDropdown).build().perform();
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement logoutLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lnkLogout")));
logoutLink.click();

预期条件失败:正在等待by.xpath:(//a[contains(text(),'Log Out')])[2](以500毫秒的间隔尝试了30秒)找到的元素的可见性。是否可以使用id重试,我已更新了答案,也不关闭浏览器并检查行为,你能看到菜单项吗?所以可能这不是对鼠标悬停事件做出反应的元素,你需要找出哪个元素是与鼠标事件绑定的,并且需要在其上执行鼠标操作。是的,这让我明白了!已找到元素,正在按预期工作。非常感谢。