So I am trying to figure out a way to automatically click on a selection elements option of choice but when I use the code provided by the selenium site for navigating options, I get the stale element exception error. I've tried using wait times to wait until the element was loaded but no matter where I put the wait time it gives me an error. It does go through the first selection and chooses an option but for the second it either goes through each and clicks then gives me an error without updating it on screen or it goes through half and gives the stale element error.
This is part of my code below:
displayed = browser.find_elements_by_xpath("//select[@name='listing_variation_id']") #check to see if this element is an option
if displayed:
selections = browser.find_elements_by_xpath("//select[@name='listing_variation_id']")
print("\n" + str(len(selections)) + "\n")
for options in selections: #select and choose an item choice
all_options = options.find_elements_by_tag_name("option")
for option in all_options:
browser.implicitly_wait(2)
print("Value is: %s" % option.get_attribute("innerText")) #debugging
option.click()
And this is the html that I am trying to navigate:
'''
<div id="variations" class="buy-box__variations ui-toolkit " data-buy-box-view-options="{"user_is_listing_owner":false,"order_already_started":false,"inline_variation_labels":false,"listing_mode":"listing_mode_default","show_preview_warning":false,"quantity_behavior":"quantity_enabled","quantity_related_nudge_is_on":true,"additional_button_class":"","is_mobile":false,"channel":1}">
<div class="buy-box__variation item-variation-option">
<label for="inventory-variation-select-0">How Many?</label>
<span>
<select id="inventory-variation-select-0" class="variation-select" name="listing_variation_id">
<option value="" selected="">Select an option</option>
<option value="44719679623">One Squeaker [$1.75]</option>
<option value="44719679633">Two Squeakers [$2.50]</option>
</select>
</span>
<div class="buy-box__variation-error p-xs-1 mt-xs-1 text-smaller bg-red text-white rounded display-none">Please select an option</div>
</div><div class="buy-box__variation item-variation-option">
<label for="inventory-variation-select-1">Placement</label>
<span>
<select id="inventory-variation-select-1" class="variation-select" name="listing_variation_id">
<option value="" selected="">Select an option</option>
<option value="42697801382">Top of Tail</option>
<option value="42697801396">Bottom of Tail</option>
<option value="42697801398">For Two- Top&Bottom</option>
<option value="42697801406">For Two- Both Bottom</option>
<option value="42697801408">For Two- Both Top</option>
</select>
</span>
<div class="buy-box__variation-error p-xs-1 mt-xs-1 text-smaller bg-red text-white rounded display-none">Please select an option</div>
</div><div class="buy-box__variation item-variation-option">
<label for="inventory-select-quantity">Quantity</label>
<span>
<select id="inventory-select-quantity" class="small" name="">
<option value="1" selected="">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</span>
<div class="buy-box__variation-error p-xs-1 mt-xs-1 text-smaller bg-red text-white rounded display-none">Please select a quantity</div>
</div>
</div>
'''
This will work for sure and it is simple.
The StaleElementException occurs when the element in question is changed on the dom and the initial reference to that element is lost by the driver.
You can search for the element again. The below code is far from usage, you may have to work on it if you decide to search for the element after stale element error