get text from element with <br> on its composition, using Python Selenium

get element by class
get element text jquery
javascript get text from div
extract text from html javascript
get element by text selenium
get text of a tag javascript
element text html
gettext in javascript

I'm pulling contact information (text) from a website and I can currently pull all the class data, using the following xpath syntax:


Using this xpath for the element, I get the following text as result:

Company Website
Phone Number

What I want to do, is to pull each of these elements individually, but the problem is that, the data is separated by <br> </br>, and I haven't had success on isolating each element.

Below is an example of the HTML structure:

<div class="col-sm-d">
a href="" target="_blank"></a>

Phone: (555) 555-5555"

The only element I am able to isolate is the website.

How can I isolate each data on this scenario?

Try to get list of text nodes as


If there are more text nodes after phone number which you don't want to use:


HTML DOM textContent Property, HTML DOM textContent Property. Get the text content of an element: var x = document. getElementById("myBtn"). textContent; Change the textual content of a <p> element with id="demo": getElementById("demo"). textContent = "Paragraph changed!"; Get all the textual content of an <ul> element with id="myList": var x = The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method. As of jQuery 1.4, the .text() method returns the value of text and CDATA nodes as well as element nodes.

First, get the elements:

var elements = _webDriver.FindElements(By.XPath(@"//*[@id='nomapdata']/div/div/div/div[2]/div[1]"));


        foreach (var element in elements)
            var temp = element.Split('\n');

            YourClass yourClass = new YourClass
                Name = temp[0],
                Title = temp[1],
                CompanyWebsite = temp[2],
                PhoneNumber = temp[3],



HTML DOM innerText Property, Example. Get the inner text of an element: var x = document.getElementById(" myBtn").innerText;. Try it Yourself �. More "Try it Yourself" examples below. Description Get the visible (i.e. not hidden by CSS) inner text of the web element, including sub-elements, without any leading or trailing whitespace. Parameters Param Param Type Mandatory Description

You can use the same locator but get the innerHTML instead of .text. This will get you all the HTML between the open and close <DIV> tags. Then you can split the resulting string by <br> and you will have all the desired pieces. From your sample HTML, it looks like you will probably want to strip() each piece to remove spaces and you will have to process/parse the link portion however you need.

s = driver.find_element_by_xpath("//*[@id='nomapdata']/div/div/div/div[2]/div[1]").get_attribute("innerHTML")
data = [item.strip() for item in s.split("<br>")]

data will now be an array of strings, e.g.

['Name', 'Title', '<a href="" target="_blank"></a>', 'Phone: (555) 555-5555']

You can then process whatever else you want/need to.

.text(), Description: Get the combined text contents of each element in the set of matched elements, including their descendants. version added: 1.0.text(). This method� Given an HTML document containing some elements and the task is to get the text inside an HTML element using JavaScript. There are two methods to get the text without HTML element which are listed below: Using innerText property; Using textContent property; Using innerText property: We can use innerText property to get the text from HTML

:contains() Selector, The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof. As with attribute value� How to get the text inside an element using jQuery. Topic: JavaScript / jQuery Prev|Next. Answer: Use the jQuery text() method. You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.

Node.textContent, Sometimes people use innerHTML to retrieve or write text inside an element, but textContent has better performance because its value is not� Property values: It contains single value text which specifies the text content of the specified node. Return value: It returns a string, representing the text of node and all its descendants. It returns null if the element is a document, a document type, or a notation.

HTMLElement.innerText, As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the� Shortest and possibly the simplest way of getting text without children's text from multiple elements (as well as for a single element — although it won't be as efficient for single elements as it is for multiple). Also introduces the possibility to include text from certain child elements whilst excluding from all others.

  • why not get them all as single string, and then split string by <br>?