首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >陈旧元素引用异常

陈旧元素引用异常
EN

Stack Overflow用户
提问于 2017-10-29 01:15:36
回答 2查看 856关注 0票数 1

因此,我试图找出一种方法来自动单击所选的选择元素选项,但是当我使用selenium站点提供的代码来导航选项时,我得到了stale element exception错误。我尝试使用等待时间来等待元素加载,但是无论我把等待时间放在哪里,它都会给我一个错误。它确实通过第一个选择并选择了一个选项,但对于第二个选项,它要么遍历每个选项并单击,然后给我一个错误,而不在屏幕上更新它,要么遍历一半并给出陈旧的元素错误。

这是我下面代码的一部分:

代码语言:javascript
复制
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:

代码语言:javascript
复制
'''
<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>
'''
EN

回答 2

Stack Overflow用户

发布于 2017-10-29 08:16:32

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

您可以再次搜索该元素。下面的代码远未使用,如果您决定搜索过时的元素错误后的元素,您可能需要对其进行修改

代码语言:javascript
复制
 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()
票数 1
EN

Stack Overflow用户

发布于 2018-09-22 22:32:08

代码语言:javascript
复制
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) {
    }
}

这肯定会起作用,而且很简单。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46992406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档