1

I am trying to fetch text from an element using WebDriver.getText() method but I am getting an empty string always.

Following are the details:

Environment: Web Browser: Chrome Version: 48.0 Chrome Driver Version: 2.20

Application: On my home page I click on a menu option that creates a drop down menu on the screen and the element, I am trying to get text from, is one of the element in this drop down menu.

DOM:

<div class="settingInfo ng-scope" ng-if="showSettings">
  <div class="settingsDropDown">
    <ul class="drop-down-menus">
      <li>some data</li>
    <div id="showProfile" data="$root.userGroup.getUserProfiles()" is-overlay-required="true" is-profile-option-required="true" extra-label="true" profile-ordering="true" class="ng-isolate-scope">
      <ul class="profileList">
          <!-- ngRepeat: profile in data | filter: { userType : 'RegularUser'} | orderBy : 'displayName' : profileOrdering --><li ng-repeat="profile in data | filter: { userType : 'RegularUser'} | orderBy : 'displayName' : profileOrdering" ng-click="togglePanel($index, profile);" class="profile-titles ng-scope setting-dropDown-firstP" id="selectProfile_0" ng-class="{'activeProfile':(profile.isActive &amp;&amp; !showProfileOption),'clickedProfile':($index == currentIndex &amp;&amp; showProfileOption),'setting-dropDown-firstP':(($index == 0) &amp;&amp; (data.length <= 3))}">
                <div class="profileCircle ng-binding">T</div>
                <div class="profileName ng-binding">Test Profile</div>
          <li>
        </ul>
      </div>

I am interested in getting the text "Test Profile" from above code.

XPATH:

I am using the following XPATH to reference the element:

//div[@id="showProfile"]//li[@id="selectProfile_0"]/div[contains(@class, "profileName")]

XPath Helper extension in chrome is telling me that my xpath is unique but when I try to do a getText on element identified by this xpath I get an empty string.

Java Code:

List<WebElement>list  = getProfileList();  //this function checks if element is visible and if it is visible it adds it in to the list and returns it.
for(Webelement e : list)
{
    System.out.println(e.getText());
}

Further to this I have tried getAttribute("innerHTML"), getAttribute("innerText") and few java scripts as suggested in this answer (to be honest I do not understand them but I have still tried them) and I did not find success.

I will really appreciate any help.

Thanks

Community
  • 1
  • 1
Gaurav
  • 43
  • 2
  • 6
  • Can you add `getProfileList()` implementation? – Guy Feb 18 '16 at 07:35
  • @guy I can not edit the post above so please see here: http://pastebin.com/ytM9fGfV (PS: the function returns the list at the end but I missed it while recreating it for public consumption). – Gaurav Feb 18 '16 at 08:00

3 Answers3

0

Try below code :

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Test Profile')]"));
String text = element.getText();
Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34
0

Try with click the "select" dropdown and try to get Note: Make sure You are in correct frame

Dinesh Kumar
  • 71
  • 1
  • 2
  • 8
0

First open the drop down, find the parent WebElement and in parent WebElement find WebElement profileName and use getText() method.

Try below code:

WebElement parentElement = driver.findElement(By.className("settingsDropDown")); 
WebElement profileName = parentElement.findElement(By.className("profileName ));
String text = profileName.getText();
Rao
  • 20,781
  • 11
  • 57
  • 77
Sandeep
  • 41
  • 1
  • 4