Selecting elements at the same node
You could do the following:
- iterate over the "list header" elements
- for each "list header", get the following siblings and collect "tags" (let's name these
li
element texts as "tags") until thelh
sibling is met
Something along these lines:
for lh in driver.find_elements_by_xpath("//ul[@id='searchresults']//lh"): restaurant = lh.text tags = [] for element in lh.find_elements_by_xpath("./following-sibling::*"): if element.tag_name == 'lh': break tags.append(element.text) print(restaurant, tags)
I figured of something simple by running some javascript in the session that will add a custom attribute with the name of the list header (LH) to each of the following list items (LI), you need to change the custom attribute name to your needs without conflicting with current attributes.
my_js = """let currentLh;document.querySelectorAll(arguments[0]).forEach(function (elem) { if (elem.tagName === "LH") { currentLh = elem.textContent.toLowerCase(); } else { elem.setAttribute("my-custom-attr", currentLh); }"""driver.execute_script(my_js, 'ul#searchresults > *')my_restaurant_list = driver.find_elemets_by_css_selector('li[my-custom-attr="restaurant"]')my_bar_list = driver.find_elemets_by_css_selector('li[my-custom-attr="bars"]')my_shopping_list = driver.find_elemets_by_css_selector('li[my-custom-attr="shopping"]')my_coffee_list = driver.find_elemets_by_css_selector('li[my-custom-attr="coffee"]')
This uses JS NodeList.forEach
function which may not be available in all browsers, if you find an issue there you will have to find a more portable looping solution for all the elements inside the unordered list.