陈旧元素引用异常 [英] Stale Element Reference Exception

查看:49
本文介绍了陈旧元素引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图找出一种自动单击选择的选择元素选项的方法,但是当我使用硒站点提供的代码导航选项时,出现了陈旧元素异常错误.我尝试使用等待时间来等待元素被加载,但是无论我把等待时间放在哪里,它都会给我一个错误.它确实经过了第一个选择并选择了一个选项,但第二次它要么经过了每个选项,然后单击,然后给了我一个错误,而没有在屏幕上更新它,要么遍历了一半,并给出了过时的元素错误.

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()

这是我要浏览的html:

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>
'''

推荐答案

当所讨论的元素在dom上更改并且驱动程序丢失对该元素的初始引用时,会发生StaleElementException.

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()

这篇关于陈旧元素引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆