I ran into the same issue and this Q&A block was the game changer. I tweaked the given code by Saifur too, to match my needs, which I believe similar to this question's request:
elementlist = []
found = False
while not found:
try:
element_ = browser.find_element_by_xpath("//*[@class='class' and (contains(text(),'Optional-text-contain'))]")
if element_.is_displayed():
value = element_.find_element_by_css_selector("*")
elementlist.append(value.text)
print("Element: " + value.text)
found = True
except NoSuchElementException:
elementlist.append("N/A")
print("Element: N/A")
found = True
This code looks for an alternating element within the page (sometimes exists, sometimes doesn't). If it exists, it gives its' value. If not - it continues normally while appending the list a "NaN" style value, just to match axis for later DFing
Again, Saifur delivered enormous assistance in this issue. Thank you user3691239 for asking this question, and mentioning it worked for you after a lil' tweak.