Sputnik
Sputnik

Reputation: 133

How can one read a list of text from divs in the most reliable way, using sibling relations in Selenium with Python?

I'm trying to locate and then read the list of text 1-5. All divs with class="col-md-6" have the same structure, so I'm trying to use the text from:

<h5>Header Unique Text</h5>

as it is the only unique element and then proceed to extract the texts but without using /div[x]/div[y] type xpath, as it won't be reliable in my case. I'm searching for a css selector(or even xpath) which uses sibling relations, maybe nth-child, related to the header tag or its parent div. However, I'm not sure you are even able to move backward in the DOM with css selectors.

Any help would be greatly appreciated.

<div class="row  dashboard-admin-widgets">
    <div class="col-md-6">...</div>
    <div class="col-md-6">...</div>
    <div class="col-md-6">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>Header Unique Text</h5>
            </div>
            <div class="ibox-content">
                <div>Text 1</div>
                <div>Text 2</div>
                <div>Text 3</div>
                <div>Text 4</div>
                <div>Text 5</div>
	    </div>
        </div>
    </div>
</div>

Upvotes: 0

Views: 114

Answers (2)

Fenio
Fenio

Reputation: 3625

You can use xpath to get the ancestor of <h5>Header Unique text</h5> like this:

//h5[normalize-space(.)='Header Unique Text']/ancestor::div[@class='ibox float-e-margins']/div[@class='ibox-content']/div

Let me break it down for you so you can adjust the xpath to your needs.

First, we look for <h5>Header Unique text</h5> and then we get its ancestor which is div[@class='ibox float-e-margins']. You can use any attribute, tag, just like you would write your xpath.

Now we are looking for elements in context of div[@class='ibox float-e-margins']. Then, use we look for all div elements with the text you desired.

Also, instead of using /ancestor::div, you can get first parent and look for it's sibling like this:

//h5[contains(text(), 'Header Unique Text')]/parent::div[@class='ibox-title']/following-sibling::div[@class='ibox-content']/div

Upvotes: 1

QHarr
QHarr

Reputation: 84465

If it is the last child as shown you can use a last-child selector

.col-md-6:last-child .ibox-content div

You could also use nth-of-type

.col-md-6:nth-of-type(n) .ibox-content div

Or even last-of-type

.col-md-6:last-of-type .ibox-content div

Upvotes: 2

Related Questions