0

I need to fetch text 'Finance' from the span tag below to compare that with another value which is saved in a file.But I am not able to fetch that.

<span class="panel-text col-md-8">Finance</span>

Here is the full code for the above

<efx-dashboard-panel> <div class="dashboard-panel dashboard-type-employee-input">
            <div class="dashboard-panel-header">
              <i class="fa fa-edit"></i>
              <span class="dashboard-panel-title">Provided Employee Input</span>
            </div>
            <div class="dashboard-panel-content">

<!----><div class="ng-star-inserted">
 <!---->
 <!----><div class="col-md-12 ng-star-inserted">
   <span class="panel-text label col-md-4">Department in which the employee works:</span><span class="panel-text col-md-8">Finance</span>
 </div>
</div><div class="ng-star-inserted">
 <!---->
 <!----><div class="col-md-12 ng-star-inserted">
   <span class="panel-text label col-md-4">Employee's Date of Birth:</span><span class="panel-text col-md-8">03-08-1990</span>
 </div>
</div><div class="ng-star-inserted">
 <!---->
 <!----><div class="col-md-12 ng-star-inserted">
   <span class="panel-text label col-md-4">County in which the employee works:</span><span class="panel-text col-md-8">Iowa</span>
 </div>
</div><div class="ng-star-inserted">
 <!---->
 <!----><div class="col-md-12 ng-star-inserted">
   <span class="panel-text label col-md-4">Employee's home address:</span><span class="panel-text col-md-8">Station Street</span>
 </div>
</div><div class="ng-star-inserted">
 <!---->
 <!----><div class="col-md-12 ng-star-inserted">
   <span class="panel-text label col-md-4">Employee ID:</span><span class="panel-text col-md-8">M1669</span>
 </div>
</div><div class="ng-star-inserted">
 <!---->
 <!----><div class="col-md-12 ng-star-inserted">
   <span class="panel-text label col-md-4">Authorization Code:</span><span class="panel-text col-md-8">45637</span>
 </div>
</div>

            </div>
          </div></efx-dashboard-panel>

The xpath selector for the above code is given below

element(by.xpath('//*[@id="employer-call"]/div[2]/div[2]/efx-tabs/div/efx-tab[4]/div/div/employee-input-panel/efx-dashboard-panel/div/div[2]/div[1]/div/span[2]'));

How can I get the text from the tag.I tried using text(),getText() and innerHTML.

RRR
  • 75
  • 2
  • 11
  • May I know the reason for negative vote? – RRR Apr 10 '18 at 11:53
  • 2
    I guess it's because 1) There is no tag for programming language/tool, 2) No code for *How exactly you tried text(),getText() and innerHTML*, 3) Provided piece of HTML is just for target element while XPath includes a lot of ancestor nodes, so there is no possibility to check whether your expression is correct or not... – Andersson Apr 10 '18 at 12:02
  • Depending on selectors that complex is a *really* bad idea. It makes your code very fragile: you won't be able to change anything in your HTML without worrying about breaking that selection. Save yourself a lot of headaches and put some identifiers into the DOM. – Daniel Beck Apr 10 '18 at 12:46
  • @DanielBeck Thanks..But I can't change that because I am a tester who is testing the application – RRR Apr 10 '18 at 12:58

2 Answers2

0

This XPath,

//span[normalize-space()="Department in which the employee works:"]
 /following-sibling::span[1]

will select the span sibling immediately following the span whose space-normalized string value is "Department in which the employee works:".

You can append /text() to select only the text nodes for the above span, or wrap the entire XPath expression in string() to take its string value.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Sorry, was still editing. It works now. Check it out. Main point is that you can select based upon a label rather than a long chain of ancestors which would be more likely to change with page updates. – kjhughes Apr 10 '18 at 13:09
  • how to get the text from the xpath? – RRR Apr 10 '18 at 13:13
  • See my note on `/text()` or `string( XPath expression )`. – kjhughes Apr 10 '18 at 13:13
  • Can someone help to find why this is not working for me – RRR Apr 11 '18 at 10:03
  • I had done `element(by.xpath('//*[@id="employer-call"]/div[2]/div[2]/efx-tabs/div/efx-tab[4]/div/div/employee-input-panel/efx-dashboard-panel/div/div[2]/div[1]/div/span[2]/text()'));` – RRR Apr 11 '18 at 10:05
  • @kjhughescan you help on the above – RRR Apr 11 '18 at 13:31
0

I got a solution by refering to

Solution

I used expect(this.deptData.getText()).to.eventually.equal(employee.department);

to fetch the data from span tag and compare that with the data in employee.department

RRR
  • 75
  • 2
  • 11