Using python.


I have a <div> element that contains a variable number of <div> elements within it. Each of these inner <div> elements contains an <a> element with an href attribute and a <span> element as a child node. I am successfully using the text within the <span> element to locate the exact <a> element I am looking for, with its associated href attribute.

At the moment I'm looping through the divs to try and return the hrefs like so:

HTML Code:
elems = driver.find_elements(By.XPATH, "insert_xpath")
for elem in elems:
    if elem.text = "element1":
        element1_href = elem.get_attribute("href")
    elif elem.text = "element2":
        element2_href = elem.get_attribute("href")
The loop is successful at finding elements with the text within the <span> tag, but when I attempt to get the href of the parent element, it returns nothing.

I'm trying to find a specific div in the DOM based on its text content, and then obtain the href within that div. If I have misunderstood the situation, I apologize.

HTML Code:
<div> #This is what I've called elems above  
    <div> #This is the first child div  
        <a href = "www.website.co.uk">  
            <span>class="someclassname"</span>
            <span>element1</span> # This is the text I am filtering on above
        </a>
    </div>
    <div> #This is the second child div
        <a href = "www.website.co.uk">  
            <span>class="someclassname"</span>
            <span>element2</span> # This is the text I am filtering on above
        </a>
    </div>
</div>
Thanks for the help in advance