Santhosh Kumar
Santhosh Kumar

Reputation: 9

How to find the XPath for outside the tag?

<div id="fileuploadsuccess" style="font-size: 14px; width: 1086px;" ng-style="{'top':alertStyle+'px','width':alertWidth+'px'}" class="autoAdjustAlert bold animated bounce ng-binding ng-scope alert alert-success" ng-class="{'alert alert-success': alert.type == 'success','alert alert-danger':alert.type == 'danger'}" ng-repeat="alert in alerts">

<a class="close" data-dismiss="alert" aria-label="close">×</a> 

Instruction successfully added.

</div>

I want the text "Instruction successfully added." alone, When I tried myself its printed like this x Instruction successfully added.

Upvotes: 0

Views: 476

Answers (3)

Prophet
Prophet

Reputation: 33371

You can remove the child node text from the total text as following:

entire_text = driver.find_element_by_xpath('//div[@id="fileuploadsuccess"]').text
child_text = driver.find_element_by_xpath('//div[@id="fileuploadsuccess"]//a[@class="close"]').text

parent_text = entire_text.replace(child_text, '')

Upvotes: 1

Siebe Jongebloed
Siebe Jongebloed

Reputation: 4870

Since there could be some whitespace-nodes as well, I would use:

//div[@id='fileuploadsuccess']/a/following-sibling::text()

Upvotes: 2

Mads Hansen
Mads Hansen

Reputation: 66781

When you select just the div element and ask for it's stringified value, it will concatenate all of the descendant text() nodes (which includes the x inside of the <a> element.

You just want the immediate text() children of that div:

//div[@id='fileuploadsuccess']/text()

Upvotes: 0

Related Questions