1

I have some HTML which looks like this one:

<div class="class1">
    <div class="some multiple classes here">
        <div class="some multiple classes here">
            <ul class="other classes">
                <li>
                    <div class="random">some text1</div>
                    <div class="random1">some text2</div>
                    <div class="random2">some text3</div>
                </li>
                <li>
                    <div class="random">some text4</div>
                    <div class="random1">some text5</div>
                    <div class="random2">some text6</div>
                </li>
                <li>
                    <div class="random">some text7</div>
                    <div class="random1">some text8</div>
                    <div class="random2">some text9</div>
                </li>
                <!-- here can appear more <li></li> elements -->
            </ul>
        </div>
    </div>
</div>

The situation is a little bit complex:

  • first, there are only a few <li></li> tags with the same <div> classes inside but different text. More, <li></li> (with the same divs) appear as time passes.

I struggled to find a solution of retrieving all some textX elements in a single loop (not necessary if not possible) using xpath and selenium but I couldn't find a away. More, when I finish all the <li></li> tags, I'd like to just wait for others to appear and take some actions.

The pseudo-code would be the following:

for mydriver.find_element_by_xpath('xpath of ul'):
    # here get all the texts
    # process them

2 Answers 2

1

You need all the divs that are direct children of li tags?

divs = driver.find_elements_by_xpath('//li/div')

Or by css selectors

divs = driver.find_elements_by_css_selector('li > div')
Sign up to request clarification or add additional context in comments.

Comments

0

You can find all the <li> tags using the <ul> and all the <div> tags using the <li> tags

divs = [] # will hold the divs
ul = driver.find_element_by_class_name('classes')
lis = ul.find_elements_by_tag_name('li') # list of the <li> tags
for li in lis:
    divs.append(li.find_elements_by_tag_name('div')) # add all the <div> tags to divs list

for div in divs:
    text = div.text

Another solution will be using contains on the "random" in the class

divs = driver.find_elements_by_css_selector('[class*="random"]')

for div in divs:
    text = div.text

7 Comments

For the first example I get an error: AttributeError: 'NoneType' object has no attribute 'text'
@Alexander it should be lis = ul.find_elements_by_tag_name('li'), I updated my answer
Well, I knew this and I modified it before but I still get the same error. Any ideas why ?
@Alexander maybe the list declaration is the problem, try divs = []
That's not the problem. The problem is that I tried to print each element you wrote, but I got: <selenium.webdriver.remote.webelement.WebElement (session="9b6579af-c027-43d6-98b4-9d14109303c0", element="{9e80dae6-dc89-4365-8306-e00aac001008}")>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.