Fabian Bosler Fabian Bosler - 3 months ago 13
HTML Question

How do I return combination of same level nodes in one webelement in selenium?

my knowledge of selenium at this point is a bit limited, but from what I understand driver.find_elements_by_xpath() returns a list of webelements. One can then iterate over the elements and do whatever one wants, like printing text.
That part is easy.
But now assume on a given page I would be looking for every combination of 3 nodes:

<div class="identifier">more stuff</div>
<h1>other text</h1>
<div class="identifier">other more stuff</div>
<h3>other text2</h3>

These 3 nodes (here h1, div with class, and h3) are on the same level of hierarchy and there are many of them there since its a list. Is there a way to have selenium return them "packaged"? In this case I could make sure I get the correct data together. The way I am doing it right now is getting the middle element and then preceding and following sibling with the specified tag. But I feel like thats whacky at best.

Thx alot.


I'm not sure what code you are using for your approach but I would do something like this.

headings = driver.find_elements_by_css_selector("parent > h1"))
for i in range(len(headings)):
    heading = driver.find_element_by_css_selector("parent > h1:nth-of-type(" + i + ")"))
    identifier = driver.find_element_by_css_selector("parent > div.identifier:nth-of-type(" + i + ")"))
    subheading = driver.find_element_by_css_selector("parent > h3:nth-of-type(" + i + ")"))
    // do something with each element here

Now you can reference each of the elements.

NOTE: This assumes that each of the elements always exists. If you are ever missing an h3, etc. this code will mismatch the groups.