How to concatenate strings within a br in python XPath?
Question: Trying to solve how to concatenate strings within a `<br>` is not working.

Here is the code:

    
    
    <li class="attr">
        <span>
            Size:L
            <br>
            Color:RED
        </span>
    </li>
    

I tried using these but is not working:

    
    
    color_and_size = row.xpath('.//li[@class="attr"][1]/span[1]/text()')[0]
    


Answer: It seems your xml structure is corrupt since no closing `</br>` tag- So if you
use `lxml` then try [soupparser](http://lxml.de/elementsoup.html) that use
Beautifulsoup- Or you can use standalone Beutifulsoup as below-

    
    
    from bs4 import BeautifulSoup
    s = """<li class="attr">
        <span>
            Size:L
            <br>
            Color:RED
        </span>
    </li>
    """
    soup = BeautifulSoup(s)
    
    print map(lambda x: x.text.strip().replace("\n",""),soup.find_all('span'))
    

Prints-

    
    
    [u'Size:L                Color:RED']
    

N.B. Beautifulsoup organises xml internally e.g. if you want valid xml of your
malformed xml then try-

    
    
    print soup.prettify()
    

Prints-

    
    
    <html>
     <body>
      <li class="attr">
       <span>
        Size:L
        <br/>
        Color:RED
       </span>
      </li>
     </body>
    </html>
    

If your `xml` was valid the below `xpath` would work-

`//li[@class='attr']/span/text()[preceding-sibling::br or following-
sibling::br]`

`[Live
Demo](http://www.xpathtester.com/xpath/467bdd7d5a59eb29289d08ea3eca9951)`
**Just click the** `Test` **button**

