Selenium: How to get text from within an html tag which has another tag in it

Tags: html,css,selenium

Problem :

<a class="spf-link current" href="/orders/returns?offset=0&limit=25">
<span class="pg-helpText">Page</span>

I need to read the value '1' in my test. When I do a get text using css-selector .spf-link.current, it gets me "Page 1". I only need '1'. How do I exclude the text from the span tag.

Solution :

You will need Javascript childNodes to get the text from the nodes.

The childNodes property returns a collection of a node's child nodes, as a NodeList object.

The nodes in the collection are sorted as they appear in the source code and can be accessed by index numbers. The index starts at 0.

WebElement element = driver.findElement(By
String node_text=(String)((JavascriptExecutor)driver)
                           .executeScript("return arguments[0].childNodes[2].nodeValue",element);

This should give you the value 1 as node_text when you run the JavascriptExecutor. Other way is to split the text Page 1 on the basis of space. But, i would prefer childNodes.

Let me know if you have any queries.

    CSS Howto..

    selectCheckboxMenu: items are shown with bold font

    How do you make a nav bar “sticky”?

    How to use css for the hover effect in another div?

    How do I make a table show a vertical scroll bar without explicitly setting the height?

    How do I use CSS in ion-list, ion-item

    How do I make the 'panel/well' dynamically wrap around the image while keeping it centered? // Using Bootswatch/Bootstrap CSS

    How to make “transbox” full width

    How do I use CSS and JS files within a HTML file being sent with Express?

    How do you select a security authentication modal with a css selector?

    How to make hover gradient on a div element like on

    How can I determine the escape sequence for characters in an icon font?

    How to turn off :hover with CSS?

    how to properly resize navbar fixed in bootstrap

    How to make this CSS hamburger menu entirely clickable?

    How do I make LessCSS output css which is then uploaded to webserver

    how do i include css based on screen size of the device

    How to Use CSS transitions

    How do you scroll the page content over a cover image (like

    How to center and span table heading (row)

    CakePHP: How to use PHP to dynamically use a CSS property type in an MVC framework

    CSS How to change background color of text?

    How to change tooltip color on open using jQuery UI Tooltip?

    How do I make a symmetrical downward pointing angle that is greater than 90 degrees using CSS?

    How to create a floating JavaScript Clock for UTC and Local Time?

    CSS - How to Draw a Multi-Coloured Line?

    How to use pure CSS to show an animation of one element's background and color property?

    How to align p tag side by side using css?

    How to show/hide content based on a css class

    How to set an auto minimum width on column with css?

    How do I overlap PNG images for a “build your own t-shirt” product display page - javascript/css/asp