1

Read a few SO threads on this but I haven't found a great answer (and one may not exist) so I thought I'd try again.

I'm trying to use Selenium with Python to retrieve text from an element, but I want to ignore some child elements within that element. The HTML looks something like this:

<div class="topclass">
  <div class="textclass"> Here is some text 
    <a class="link1"> with a link here </a>
    <a class="link2"> and another link here. </a>
  and more text.
  </div>
</div>

What I need is to retrieve the text "Here is some text and more text" where I ignore the text of the two child link nodes. A couple restrictions:

  • The link nodes can be anywhere within the text and there can be any number of them.
  • I want to avoid the "get all the text and then parse it within Python" solution if possible (which has been suggested).
  • There's nothing unique about the example text here so the solution has to be general to all types of text and can't be coded to the text content itself.

Is there a way to do what I need with Selenium/Xpath?

plam
  • 1,305
  • 3
  • 15
  • 24
  • 1
    http://stackoverflow.com/questions/12325454/how-to-get-text-of-an-element-in-selenium-webdriver-via-the-python-api-without – Grasshopper Nov 03 '16 at 06:55
  • Thanks! Didn't see that one! – plam Nov 03 '16 at 07:29
  • Possible duplicate of [How to get text of an element in Selenium WebDriver (via the Python api) without including child element text?](https://stackoverflow.com/questions/12325454/how-to-get-text-of-an-element-in-selenium-webdriver-via-the-python-api-without) – C8H10N4O2 Mar 02 '19 at 20:20

0 Answers0