0

I am trying to process all elements which have text. I know I can iterate over the list and process only those which contain text:

from selenium import webdriver driver = webdriver.Chrome() my_elelements = driver.find_elements_by_xpath("//*") for elm in my_elements: if elm.text!="" 'processing of text 

but is there a quicker way only find elements with text, with selenium's driver.find_elements_by_ without finding all elements and then filtering only those with text...? Is there a wildcard that can be used here ?

3
  • 1
    You could use XPath's not() function. stackoverflow.com/questions/23779467/… Commented Dec 5, 2017 at 6:45
  • What exactly do you mean by only those with no text? Can you show us how you do that? Commented Dec 5, 2017 at 6:46
  • I clarified the question. I want to filter out those events which have no text, and only process those which have some text - any text Commented Dec 5, 2017 at 7:07

1 Answer 1

2

You can use the following x-paths to get all elements with text.

  1. //*[text()] -- it will returns all element with text including parent nodes
  2. //*[text()][count(*)=0] -- it returns all element with text only child nodes

or you can use the following x-path to get the all elements without text.

  1. //*[not(text())] -- it will returns all element without text including parent nodes
  2. //*[not(text())][count(*)=0] -- it returns all element without text only child nodes
Sign up to request clarification or add additional context in comments.

1 Comment

The XPath //*[text()] is going to return some elements with no text (those with just a line break), the elements with text but hidden, and all the text within a <script> or <style>. You could improve it by formatting the white spaces and by starting after the body with html/body//*[text()[normalize-space()]].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.