指定的父元素不再附加到DOM

2024-09-10 07:06:03 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Python,我想选择select标记的每个选项,因为以下HTML代码:

<SELECT onchange="top.selectionMediator.campaignSPListBoxChanged();window.campaignSPPeopleFilterBox_listbox.onSelect('LIST_ITEM_ACTION',this);; ; newdd_ipadOnChange(this, event);" onclick="newdd_onListClick(this, event)" id=campaignSPPeopleFilterBox_listbox class=bpDropDownList onkeydown="newdd_onListKeyDown(this, event)" style="WIDTH: 175px; VISIBILITY: hidden" size=4 name=campaignSPPeopleFilterBox_listbox maxWidth="175" index="0">
    <OPTION value=-1 wasSelected="false" wasSelectedTmp="false">-Aucune campagne-</OPTION>
    <OPTION value=163 wasSelected="false" wasSelectedTmp="false">campagne1</OPTION>
    <OPTION value=95 wasSelected="false" wasSelectedTmp="false">campagne2</OPTION>
    <OPTION selected value=113 wasSelected="false" wasSelectedTmp="true">campagne3</OPTION>
</SELECT>

我编写了以下代码:

from selenium import webdriver
from selenium.webdriver.ie.options import Options
from selenium.webdriver.support.select import Select

ieOptions = Options()
ieOptions.ignore_protected_mode_settings = True
browser = webdriver.Ie(options=ieOptions)

select = Select(browser.find_element_by_id("campaignSPPeopleFilterBox_listbox"))
for i in range(1,len(select.options)):
    browser.find_element_by_id("campaignSPPeopleFilterBox_listbox_0Button").click()
    # select.options[i].click()
    optionText = select.options[i].text
    browser.find_element_by_xpath("//select[@name='campaignSPPeopleFilterBox_listbox']/option[text()='" + optionText + "']").click()
    # Do something

我设法选择第一项,但当FOR循环转到第二项时,我收到以下错误消息:

selenium.common.exceptions.StaleElementReferenceException: Message: Specified parent element is no longer attached to the DOM

你能帮我解决这个问题吗

提前感谢您的帮助


Tags: browserfalsevalueseleniumelementthisselectoptions