尝试 select 下拉选项时出现错误 "Message: stale element reference: element is not attached to the page document"
Error "Message: stale element reference: element is not attached to the page document" when trying to select option of drodown
select = Select(self.driver.find_element_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select"))
options = len(self.driver.find_elements_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option"))
m = 1
while m <= options:
value = self.driver.find_element_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]").get_attribute("value")
text = self.driver.find_element_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]").text
select.select_by_value(value)
m = m + 1
当 运行 代码出现以下错误时:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=94.0.4606.71)
HTML代码如下:
<div class="select-field">
<select name="group[14]" data-ajax-select-variants="true">
<option selected="selected" value="211">
1 Jahr
</option>
<option value="209">
3 Jahre
</option>
<option value="348">
5 Jahre
</option>
</select>
</div>
现在,它 select 的前两个选项没有任何问题,但是当它尝试 select 第三个选项时,它给了我上面显示的错误。
我试过了:
- Select通过“select_by_visible_text”选择选项
- 通过 Select().options
获取下拉选项
- for 循环 Select().options
- Select通过“select_by_index”选择选项
- 在select取消选择新选项之前
selenium.common.exceptions.StaleElementReferenceException: Message:
stale element reference: element is not attached to the page document
(Session info: chrome=94.0.4606.71)
表示对元素的引用现在是“陈旧的”--- 元素不再出现在页面的 DOM 上。这种期望的原因可能是您的 DOM
已更新或刷新。例如,执行 click()
等操作后,您的 DOM
可能会更新或刷新。此时当您尝试在 DOM
上查找元素时,您将遇到此错误。
您必须在更新或刷新中重新找到该元素 DOM
try:
element1 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
value = element1.get_attribute("value")
element2 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
text = element2.text
select.select_by_value(value)
except StaleElementReferenceException:
element1 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
value = element1.get_attribute("value")
element2 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
text = element2.text
xPath
是你用的不靠谱。如果 DOM
结构有任何变化,它将来可能会失败。
select = Select(self.driver.find_element_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select"))
options = len(self.driver.find_elements_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option"))
m = 1
while m <= options:
value = self.driver.find_element_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]").get_attribute("value")
text = self.driver.find_element_by_xpath("/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]").text
select.select_by_value(value)
m = m + 1
当 运行 代码出现以下错误时:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=94.0.4606.71)
HTML代码如下:
<div class="select-field">
<select name="group[14]" data-ajax-select-variants="true">
<option selected="selected" value="211">
1 Jahr
</option>
<option value="209">
3 Jahre
</option>
<option value="348">
5 Jahre
</option>
</select>
</div>
现在,它 select 的前两个选项没有任何问题,但是当它尝试 select 第三个选项时,它给了我上面显示的错误。
我试过了:
- Select通过“select_by_visible_text”选择选项
- 通过 Select().options 获取下拉选项
- for 循环 Select().options
- Select通过“select_by_index”选择选项
- 在select取消选择新选项之前
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=94.0.4606.71)
表示对元素的引用现在是“陈旧的”--- 元素不再出现在页面的 DOM 上。这种期望的原因可能是您的 DOM
已更新或刷新。例如,执行 click()
等操作后,您的 DOM
可能会更新或刷新。此时当您尝试在 DOM
上查找元素时,您将遇到此错误。
您必须在更新或刷新中重新找到该元素 DOM
try:
element1 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
value = element1.get_attribute("value")
element2 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
text = element2.text
select.select_by_value(value)
except StaleElementReferenceException:
element1 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
value = element1.get_attribute("value")
element2 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
text = element2.text
xPath
是你用的不靠谱。如果 DOM
结构有任何变化,它将来可能会失败。