简体   繁体   中英

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.

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

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

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, '')

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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