Not able to get the text from the drop down

101 views
Skip to first unread message

sandy

unread,
May 10, 2022, 6:31:25 AM5/10/22
to Selenium Users
I am trying to get the Xpath of the drop down menu as iam doing a menu comparison.

But getting a error:

 [object Text]. It should be an element.

Below is my Xpath:

WebElement submenuitem = menuLink.findElement(By.xpath("//button//text()"));

Had Also tried with the below Xpath:

List<WebElement> list = menuLink.findElements(By.xpath("//div[contains(@class,'mat-list-item-content')]"));
                           
                    String element = list.get(0).getAttribute("innerText");

but this fetches only the first text from the drop down ..
Dropdown menu has :

Vessel Database
Partner Database
User Management
Export to Excel

So i need to read these dropdown menus ... Iam doing a menu bar comparison so cannot hard cord the text ...Any Leads will be helpful

HTML:

<div _ngcontent-bhg-c40="" class="ng-star-inserted"><mat-icon _ngcontent-bhg-c40="" role="img" aria-hidden="false" aria-label="dropdown" class="mat-icon notranslate dropdown-icon material-icons mat-icon-no-color" data-mat-icon-type="font">expand_more</mat-icon><mat-list _ngcontent-bhg-c40="" class="mat-list mat-list-base dropdown-list"><button _ngcontent-bhg-c40="" mat-list-item="" class="mat-list-item mat-focus-indicator dropdown-button ng-star-inserted" type="button"><div class="mat-list-item-content"><div mat-ripple="" class="mat-ripple mat-list-item-ripple"></div><div class="mat-list-text"></div>**Vessel Database**</div></button><button _ngcontent-bhg-c40="" mat-list-item="" class="mat-list-item mat-focus-indicator dropdown-button ng-star-inserted" type="button"><div class="mat-list-item-content"><div mat-ripple="" class="mat-ripple mat-list-item-ripple"></div><div class="mat-list-text"></div>**Partner Database**</div></button><button _ngcontent-bhg-c40="" mat-list-item="" class="mat-list-item mat-focus-indicator dropdown-button ng-star-inserted" type="button"><div class="mat-list-item-content"><div mat-ripple="" class="mat-ripple mat-list-item-ripple"></div><div class="mat-list-text"></div>**User Management**</div></button><button _ngcontent-bhg-c40="" mat-list-item="" class="mat-list-item mat-focus-indicator dropdown-button ng-star-inserted" type="button"><div class="mat-list-item-content"><div mat-ripple="" class="mat-ripple mat-list-item-ripple"></div><div class="mat-list-text"></div>**Export Excel**</div></button><!----></mat-list></div>

SuperKevy

unread,
May 10, 2022, 2:20:30 PM5/10/22
to Selenium Users
Consider a for loop where you get the len() or element count of the array returned to List.   
see example at 

SuperKevy

unread,
May 10, 2022, 2:31:37 PM5/10/22
to Selenium Users
Java uses .size()   
Ruby/Python are len() 

sandy

unread,
May 10, 2022, 11:14:04 PM5/10/22
to Selenium Users
Its Java did add for loop but it is not working 

Veena

unread,
May 11, 2022, 12:10:19 AM5/11/22
to Selenium Users
Check the list size before your loop.

SuperKevy

unread,
May 11, 2022, 10:43:30 AM5/11/22
to Selenium Users
I don't normally work with bloated Java.
This is probably the easiest of the examples:
 
for(int i=0; i<list.size(); i++){
            System.out.println(list.get(i).getAttribute("innerText"););
        }
Print out the list.size() it should show (4).  If its not then you have the wrong element in your xpath: of menuLink.findElements(By.xpath...............
it could be your looking for the innerText of the button collection within the dropdown container code.     You'll figure it out!
Reply all
Reply to author
Forward
0 new messages