skip to Main Content

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

2

Answers


  1. Try text() option, it will return the text present in the li tag

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

    OR

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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search