horace_vr
horace_vr

Reputation: 3166

Python Selenium - finding elements which contain any text

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 ?

Upvotes: 0

Views: 1169

Answers (1)

Murthi
Murthi

Reputation: 5347

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

Upvotes: 2

Related Questions