Jaspreet Singh
Jaspreet Singh

Reputation: 1

I want to only extract the content of li without the contents of span in Python Selenium WebDriver

So here is the sample html doc:

<div id="div1">
    <ul class="lists">
        <li class="listItem1"> List content 
            <span class="span1">span text</span>
        </li>
    </ul>
</div>

I only want to extract "List content"

If I do this:

elementList = driver.find_element(By.XPATH, "/div/ul/li")
elementList.text  

I get : List content span text

If I do this:

elementList = driver.find_element(By.XPATH, "/div/ul/li[1]")

I still get List content span text

What can I do to only get "List content" without the span text

Upvotes: 0

Views: 62

Answers (1)

Ship_Guy
Ship_Guy

Reputation: 21

elementList = driver.find_element(By.XPATH, "//li[@class='listItem1']").text
span_text = driver.find_element(By.XPATH, "//span[@class='span1']").text
elementList = elementList.replace(span_text, "")

This might be a little long winded, but I think it should do the trick.

Upvotes: 2

Related Questions