There is nesting:
<div class = "top">
<div>
<span > </span>
<span > </span>
<span > </span>
</div>
</div>
<div >
<div>
<span > </span>
<span > </span>
<span > </span>
</div>
</div>
It is necessary to take the "bottom" elements in blocks one by one, based on the "top", but this entry returns an empty list.
for i in range(2):
elements = browser.find_elements_by_xpath("//div[@class='top'][" str(i) "]//span[@class='bottom']")
print(elements)
What's my mistake?
CodePudding user response:
To get bottom elements per top blocks here you can use this XPath:
for i in range(1,3):
elements = browser.find_elements_by_xpath("(//div[@class='top'])[" str(i) "]//span[@class='bottom']")
CodePudding user response:
Why not retrieve all elements and iterate over them instead?
elements = browser.find_elements_by_xpath('//div[@]/span[@]')
for element in elements:
if element: # some invalidating condition
continue
print(element) # success!
This way selectors are much more efficient as you onyl need to execute 1 selector rather than N selectors. Also, note that xpath is pretty powerful - maybe you can integrate selection logic in the xpath itself?
