Python selenium - get values from p and span

Viewed 294

Let's say I have a code like this:

<div class="div1">
            <div class="div2">
            <div class="div3">X:  <span class="SpanX">ValueX</span></div>
            <p class="p_class">A: <span class="SpanA"> ValueA </span> </p>
            <p class="p_class_2">B: <span class="SpanB">ValueB</span></p>
            <p class="p_class">C: <span class="SpanA">ValueC</span></p>
            </div>
</div>      

I would like to get a dictionary like {X:ValueX, A:ValueA, B:ValueB, C:ValueC}

I tried to get driver.find_elements_by_xpath('//div[@class = "div1") but what next?

How do I go deeper in the structure? How do I retrieve values of span nested in p ?

3 Answers

Copy the xpath of each of the value you want to retrieve. You will find a pattern there and a simple loop can be used to iterate those elements.

For example, the xpaths of elements inside the tag might look like these:

- /html/body/div/div/p[1]/span
- /html/body/div/div/p[2]/span
- /html/body/div/div/p[3]/span

Following this pattern, you can use the following loop to extract your desired value:

for index in range(1,4):

    # concatenating the index values to iterate through the elements
    xpath_string = "/html/body/div/div/p[" + str(index) + "]/span"
  
    # catching that particular element using xpath
    element = driver.find_element_by_xpath(xpath_string)

In XML your code I see the first text pair is inside <div class="div3"> while all the other text pairs are inside <p> elements. So the common denominator here is that all these elements are immediate children of <div class="div2"> element.
Now you can get the text value of the direct child elements and of the internal span elements.
You can get a list of "key" values, a list of "value" values and then zip them into a dictionary as following:

keys = []
values = []
elements = driver.find_elements_by_xpath("//div[@class='div2']/*")
for element in elements:
    entire_text = element.text
    key = entire_text.split(" ")[0]
    keys.append(key)
elements = driver.find_elements_by_xpath("//div[@class='div2']/*/span")
for element in elements:
    value = element.text
    values.append(value)
dictionary = dict(zip(keys, values))
print(dictionary) #{X:ValueX, A:ValueA, B:ValueB, C:ValueC}
Related