How to extract the text content of an element using XPath in Selenium?

I have a setup that looks like this :

<div class="content">
    <h3 class="faq">Joocey</h3>
        Locked Bag 4005
        <br> 
        6/78 William Street
        <br>
        Sydney NSW 2000
        <br> Customer Care: <a href="tel:1300886534">1300886534</a><br>
        or<br> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4028252c30002a2f2f2325396e232f2d6e2135">[email protected]</a><br> <br>
        <div class="greenbar">
            <a href="/Stores">See All Stores <i class="icon-chevron-right icon-white pull-right">    </i</a>
        </div>
                    <!--greenbar-->

                    <!--main_footer-->
</div>

How do I single out each individual text line within the div[@class="content"] ?

For instance, I need to extract : "Joocey", "Locked Bag 4005", "6/78 William Street", "SydneyNSW 2000", "Customer Care:", "1300886534".

I attempted

"//div[@class='content'][text()[2]]"
to specifically target "Locked Bag 4005" but it ends up selecting all the text. Any assistance would be highly appreciated.

Answer №1

When using DOM selectors to choose text, there is no consideration for individual "lines".

To work with the text within div[@class="content"], you will need to retrieve all of it and treat it as a string. Divide it based on new-line characters, then access the array that is produced.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions for utilizing lodash or JavaScript to apply a filter to an object

Received this object from the Server: var data = { test1: { documents: [] }, test2: { documents: [{ vId: 'sdfas23', TypeId: '81', isDeleted: false }], answer: true }, test3: { documents: ...

The "click" function is functioning properly; however, an error is being thrown indicating that the element is not visible at the moment

When attempting to click a drop-down list item using cssselector, xpath, and linktext, the element is successfully clicked but an ElementNotVisibleException exception is thrown simultaneously. There are no duplicate elements in the DOM. I have experimente ...

Is there a way to show the Username across multiple login pages without using PHP?

Is there a way to dynamically display the username on the Homepage by extracting it from the URL? <div id="login"> <form method="post" action=""> <h2>Login</h2> <p> <label for="username"&g ...

Illustrator export script for efficient saving of images as web-friendly jpg files

Looking for assistance with creating a script in illustrator CC2017 that can automatically export files to web (legacy) as JPG, save the file, and then close it. I have 700 files, each with 2 art boards, and manually exporting and saving each one is time ...

Struggling to hide empty columns in an HTML table with jQuery - it's just not cooperating!

<html> <head> <script src="jquery-1.4.4.js"></script> <script> $('table').each(function(a, tbl) { var currentTableRows = $(tbl).attr('rows').length - 1; $(tbl).fin ...

Ensure the item chosen is displayed on a single line, not two

I've encountered an issue with my select menu. When I click on it, the options are displayed in a single line. However, upon selecting an item, it appears on two lines - one for the text and another for the icon. How can I ensure that the selection re ...

jQuery's callback handling often reverses the statement I just made

My current project involves using AJAX with jQuery to send a get request. Once the request is successful, I insert the response into a specific div: $.get("content.php", function(result) { $('#content').html(result); The content I'm fetc ...

Vue.js 2 view failing to update after modifying data in Vuex state

Greetings, I am currently working on developing a table to monitor the validation status of multiple items. Below is the VueX store configuration: mutations: { ..., set_scaninfos: function (state, scaninfos) { Vue.set(state, 'scaninfos&a ...

When the page is refreshed, the ContextProvider fails to mount

For the complete code, you can view it on codesandbox. Within my countries API application, I have implemented two Route components - <> <Header/> <Router> <Switch> <CountriesDataProvider> ...

Creating a function in Angular to locate each object based on its ID

Hello there, I am currently working on creating a method called findChildByIdInData(data:any, childId:string). This method will take in a JSON main node with children that have unique IDs, and the goal is to find a specific object based on the ID provided ...

Refresh Rails 4 instance variables seamlessly without reloading the page

Is there a method to update an instance variable in the view without refreshing the page? I'm using AJAX to post and create a new record. After creating the record, I want it to be added to the current instance variable. Let's say I have an act ...

If I don't utilize dependency injection in Angular, it prompts me for arguments

Attempting to implement a service like this but encountering some issues translateService = new TranslateService(); An error message pops up stating that there are 9 missing arguments. However, when I modify it to look like this constructor(private trans ...

Is there a way to use JavaScript to open a new window that appears on top of all others?

When you open a window like this alongside a notepad, the new window appears below the notepad. I am curious about how to make a new window open on top of all other windows. Using window.focus() does not seem to do the trick.. setTimeout(function() { ...

What is the correct way to use setInterval in a React component's constructor?

I'm trying to set an interval when the main component renders. I attempted to do this in the constructor like so: constructor(props) { super(props); this.props.fetchUserInfo(); this.props.fetchProducts(); setInterval(console.log(&a ...

Can you explain the concept of binding and unbinding in jQuery?

Can you explain the concepts of binding and unbinding in jQuery in simple terms for someone who learns slowly? ...

Can I create interactive stacked shapes with CSS and/or JavaScript?

Trying to explain this may be a challenge, so please bear with me. I need to create an "upvote" feature for a website. The number of upvotes is adjustable in the system settings. The upvote controls should resemble green chevrons pointing upwards. For exa ...

Looking to retrieve the raw HTTP header string in Node.js using express.js?

Currently, I am utilizing Node.js and express.js for my work. The project I am currently working on requires me to access the raw strings of the HTTP headers (charset and accepted). In express.js, there is a function available that can provide the charset ...

"Caution: Refs cannot be assigned to function components" message encountered when utilizing a custom component in Next.js

I created a component called HeaderIcon with the following code: function HeaderIcon({ inactiveIcon, activeIcon }) { const [isActive, setIsActive] = useState(false); return ( <div onClick={() => setIsActive(!isActive)}> {isActive ? ...

Posting an image to Instagram with the help of Selenium

I'm looking to upload an image + text using Selenium, and I came across a solution on StackOverflow The challenge I'm facing is that AutoIt only works on Windows, and I need to deploy the Bot on a Linux server... Everything runs smoothly until ...

What is the process for retrieving a documentDB document using Azure functions?

After setting up an Azure function and a DocumentDB database with a users collection, I find myself at a roadblock trying to establish the connection between the two. My intention is to input a username and have the function automatically retrieve the corr ...