0

I have a menu in the application when you click on any menu item it gives you a list which looks like this:

<ul class="list-unstyled components">
<ul class="list - unstyled components"> 
<li class="active"><a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle blueMenu">
<i class="nav-icon fa fa-angle-double-right"></i>
Audit Reports Tracking</a>
</li>
<li><a href="/Pages/Main_Page/Audit_Report.aspx?pageId=10" class="nav-link">
<i class="fa fa-angle-double-right"></i>&nbsp;<span style="font-size: small">
Audit Report Entry</span> </a>
</li>
<li><a href="/Pages/Monitor/Audit_Report_Monitor.aspx?pageId=10" class="nav-link">
<i class="fa fa-angle-double-right">
</i>&nbsp;<span style="font-size: small">
 Audit Escalation Monitor</span> </a>
</li>
<li><a href="/Pages/FinancialLoss/FinancialImpactsReport.aspx?pageId=10" class="nav-link">
<i class="fa fa-angle-double-right"></i>
 &nbsp;<span style="font-size: small">
 Financial Impacts Report</span> </a>
 </li> 
 </ul> 
 </ul>

So i wrote a code like that : to traverse the sub menu no matter what the size of the sub menu in case it became shorter or longer in the future or for different user role logins becaus in the application the sub menus differes for each user role.

Here's the code:

WebElement auditMenu = driver.findElement(By.xpath("//*[@id=\"MainMenuDiv\"]/ul"));

    List<WebElement> links = auditMenu.findElements(By.tagName("li"));

    for (int i = 1; i < links.size(); i++) {

        System.out.println(links.get(i).getText());

        if (links.get(i).getText().equals("Audit Escalation Monitor")) {

            System.out.println("hi");

        }

    }

The if Statement is not executing and I don't know what is the reason for it.

Thanks in advance

Muskan Arora
  • 56
  • 11
Ezz94
  • 35
  • 5

2 Answers2

1

If you can get the links with correct data. The "if" statement cannot be executed due to the encoding on the text "Audit Escalation Monitor". Can you try to copy that text directly from the website and replace it into your source code. I sometime met that error, so that solution may work.

W332
  • 11
  • 1
  • i have done that too didn't work but if i remove the if statment it will interact normally – Ezz94 Jan 11 '21 at 12:06
  • 1
    Actually, your code gets the text for the "li" tag, and the .getText() function will get "Audit Escalation Monitor" and the " " also because of getting all text inside the "li" tag. So the text will not equal "Audit Escalation Monitor". That is the reason why you can use the "contains" function like the below answer. The " " mean non-breaking space. The way you can do this is to open the console on Chrome and get the text by javascript directly and replace it with "Audit Escalation Monitor" from your if statement. – W332 Jan 12 '21 at 08:12
0

I have done it guys i used contain instead of equals :

  if (links.get(i).getText().contains("Audit Escalation Monitor")) {

        System.out.println("hi");

    }
Ezz94
  • 35
  • 5