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
When running the Code i get the following error:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=94.0.4606.71)
The HTML Code is the following:
<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>
As it is now, it select's the first two options without any Problems, but when it tries to select the third option it gives me the above shown error.
I tried:
- Selecting the option via "select_by_visible_text"
- getting the options of the dropdown via Select().options
- for loop with Select().options
- Selecting the options via "select_by_index"
- Before selecting the new option deseleccting all
CodePudding user response:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=94.0.4606.71)
Indicates that a reference to an element is now "stale" --- the element no longer appears on the DOM of the page. The reason for this expectation is may be your DOM
got updated or refreshed. For an example, after performing an action like click()
your DOM
may get updated or refreshed. In this time when you are trying to find an element on DOM
you will experience this error.
You have to re-find that element in updated or refreshed 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
The xPath
are what you are using are not reliable. It may fail in future if there is any change in DOM
structure.