【发布时间】:2016-05-30 06:02:37
【问题描述】:
我正在尝试使用 WebDriver.getText() 方法从元素中获取文本,但我总是得到一个空字符串。
以下是详细信息:
环境: 网络浏览器:Chrome 版本:48.0 Chrome 驱动版本:2.20
应用: 在我的主页上,我单击一个菜单选项,该选项会在屏幕上创建一个下拉菜单,而我试图从中获取文本的元素是此下拉菜单中的元素之一。
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>
我有兴趣从上面的代码中获取文本“测试配置文件”。
XPATH:
我正在使用以下 XPATH 来引用该元素:
//div[@id="showProfile"]//li[@id="selectProfile_0"]/div[contains(@class, "profileName")]
chrome 中的 XPath Helper 扩展告诉我我的 xpath 是唯一的,但是当我尝试对由此 xpath 标识的元素执行 getText 时,我得到一个空字符串。
Java 代码:
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());
}
除此之外,我还尝试了 getAttribute("innerHTML")、getAttribute("innerText") 和 this answer 中建议的一些 java 脚本(老实说,我不理解它们,但我仍然尝试过它们)并且我没有找到成功。
我将非常感谢任何帮助。
谢谢
【问题讨论】:
-
你能添加
getProfileList()实现吗? -
@guy 我无法编辑上面的帖子,所以请看这里:pastebin.com/ytM9fGfV(PS:函数在最后返回列表,但我在重新创建它以供公众使用时错过了它)。跨度>
标签: javascript java selenium xpath selenium-webdriver