Using BeautifulSoup, is it possible to move to the parent tag when using the search for text function?
<p>Is it possible to move from the current position in the DOM up and down when only the text is an common identifier?</p>

<pre><code>&lt;div&gt;changing text&lt;/div&gt;
    &lt;div&gt;fixed text&lt;/div&gt;
</code></pre>

<p>How to get the text <code>changing text</code> when searching for the <code>fixed text</code> and moving up to parent div? </p>

<p>What I tried:</p>

<pre><code>x = soup.body.findAll(text=re.compile('fixed text')).parent

AttributeError: 'ResultSet' object has no attribute 'parent'
</code></pre>

<p>This program might do what you want:</p>

<pre><code>from bs4 import BeautifulSoup
import re

html = '&lt;body&gt;&lt;div&gt;changing text&lt;/div&gt;&lt;div&gt;fixed text&lt;/div&gt;&lt;body&gt;'

soup = BeautifulSoup(html)

x = soup.body.findAll(text=re.compile('fixed text'))[0].parent.previous_sibling

assert x.text == 'changing text'
</code></pre>

python beautifulsoup