How to select an element inside div->ul->li in Selenium

Viewed 1359

I have a menu which displays a list on mouse hover, I want to click on Logout. I've written some code but unable to get the desired result.

Here's my Java code:

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();

  }

I've tried many things, you can see the lines of code that are commented. Nothing works for me.

Here's my HTML Code, of which I'm performing automation for

<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>

It is able to find the "profile-dropdown" element but then throws the exception and unable to find the list element.

Please Help.

1 Answers
Related