Stale Element Reference Exception

2020-08-03 04:33发布

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="{&quot;user_is_listing_owner&quot;:false,&quot;order_already_started&quot;:false,&quot;inline_variation_labels&quot;:false,&quot;listing_mode&quot;:&quot;listing_mode_default&quot;,&quot;show_preview_warning&quot;:false,&quot;quantity_behavior&quot;:&quot;quantity_enabled&quot;,&quot;quantity_related_nudge_is_on&quot;:true,&quot;additional_button_class&quot;:&quot;&quot;,&quot;is_mobile&quot;:false,&quot;channel&quot;: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&amp;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>
'''

2条回答
在下西门庆
2楼-- · 2020-08-03 04:45
for(int i =0; i<2;i++) {

    try {
    WebElement se = driver.findElement(By.xpath("desired xpath"));
    js.executeScript("arguments[0].click()", se);
    break;
    }
    catch(StaleElementReferenceException e) {
    }
}

This will work for sure and it is simple.

查看更多
神经病院院长
3楼-- · 2020-08-03 04:49

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

 from selenium.webdriver.support.select import Select as WebDriverSelect
 options = WebDriverSelect(driver.find_elements_by_xpath("//select[@name='listing_variation_id']")).options

for i in range(len(options)):
   try:
      options[i].click()
   except StaleElementReferenceException:
       options = WebDriverSelect(driver.find_elements_by_xpath("//select[@name='listing_variation_id']")).options
        if not options.is_selected():
          options[i].click()
查看更多
登录 后发表回答