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 && !showProfileOption),'clickedProfile':($index == currentIndex && showProfileOption),'setting-dropDown-firstP':(($index == 0) && (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
Try below code :
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Test Profile')]"));
String text = element.getText();
尝试单击“选择”下拉列表,然后尝试获取注释:确保您位于正确的框架中
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.