繁体   English   中英

无法使用Selenium获取元素的文本

[英]Not able to get text for an element using Selenium

我正在尝试使用WebDriver.getText()方法从元素中获取文本,但是我总是得到一个空字符串。

以下是详细信息:

环境:Web浏览器: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 &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>

我有兴趣从上述代码中获取文本“ Test Profile”。

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”)和此答案中建议的一些Java脚本(说实话,我不理解它们,但我仍然尝试过),但没有找到成功。

我真的很感激任何帮助。

谢谢

试试下面的代码:

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Test Profile')]"));
String text = element.getText();

尝试单击“选择”下拉列表,然后尝试获取注释:确保您位于正确的框架中

首先打开下拉菜单,找到父WebElement,然后在父WebElement中找到WebElement profileName并使用getText()方法。

试试以下代码:

WebElement parentElement = driver.findElement(By.className("settingsDropDown")); 
WebElement profileName = parentElement.findElement(By.className("profileName ));
String text = profileName.getText();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM