S
S
semolex2014-09-13 13:05:44
Python
semolex, 2014-09-13 13:05:44

How to make a loop to check for the presence of text in an element (JavaScript, Selenium Python)?

Hello!
I have a question like this:
Selenium Python method finds an element:
price = driver.find_by_css_selector('.price')
Checks if there is text:

if price.text is None:
    driver.execute_script(here_is_the_script)

So, I need the JS script itself, which would go through all the child elements of our element if there was no text in it and stop if there was text in one of them.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cawabunga, 2014-12-22
@Cawabunga

(function (el){
    var children = el.childNodes, i = children.length;
    while (i--) {
        if (children[i].nodeType === 1 && children[i].innerHTML) {
            return;
        }
    }
}(document.querySelectorAll(".price")[0]))

Simple traversal of first level child elements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question