简体   繁体   English

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

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

I am trying to fetch text from an element using WebDriver.getText() method but I am getting an empty string always. 我正在尝试使用WebDriver.getText()方法从元素中获取文本,但是我总是得到一个空字符串。

Following are the details: 以下是详细信息:

Environment: Web Browser: Chrome Version: 48.0 Chrome Driver Version: 2.20 环境:Web浏览器:Chrome版本:48.0 Chrome驱动程序版本: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: 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. 我有兴趣从上述代码中获取文本“ Test Profile”。

XPATH: XPATH:

I am using the following XPATH to reference the element: 我正在使用以下XPATH来引用元素:

//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. chrome中的XPath Helper扩展告诉我xpath是唯一的,但是当我尝试对此xpath标识的元素执行getText时,会得到一个空字符串。

Java Code: 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());
}

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

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. 首先打开下拉菜单,找到父WebElement,然后在父WebElement中找到WebElement profileName并使用getText()方法。

Try below code: 试试以下代码:

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