我知道这不是新错误,但我已经完成了关于此主题的StackExchange和StackOverflow的所有答案,但仍然无法解决StaleElementException。我使用了隐式和显式等待(参见代码注释)。请提供解决方案,也请在回答问题之前访问该网站,这样您就可以准确了解我的目标。我想从dropdown元素中获取所有数据(是的,也尝试了Select类)。这里的问题是StaleElementException。问题不在于下拉,而是StaleElementException我似乎无法摆脱它。请帮助。提前谢谢。
代码:
`PinId = 'ctl00_Contentplaceholder2_ddlPin'
RoadId = 'ctl00_Contentplaceholder2_ddlRoadName'
BuildingId = 'ctl00_Contentplaceholder2_ddlBuildingName'
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get('https://www.bestundertaking.net/BuildingWiseConsumerDetails.aspx')
PinSelect = driver.find_element_by_id(PinId) # WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, PinId)))
PinValues = [x for x in PinSelect.find_elements_by_tag_name("option")]
try:
for p in PinValues:
if p.get_attribute("value") is None or p.get_attribute("value") == '' or p.get_attribute("value") == ' ':
continue
PinSelect = driver.find_element_by_id(PinId) # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, PinId)))
PinSelect.send_keys(p.get_attribute("value"))
driver.implicitly_wait(50)
time.sleep(2)
RoadValues = driver.find_element_by_id(RoadId)
roadoptions = [x for x in RoadValues.find_elements_by_tag_name("option")] # Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, RoadId))))
for o in roadoptions:
if o.get_attribute("value") is None or o.get_attribute("value") == '' or o.get_attribute("value") == ' ':
continue
RoadSelect = driver.find_element_by_id(RoadId) # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, RoadId)))
RoadSelect.send_keys(o.get_attribute("value"))
driver.implicitly_wait(50)
time.sleep(2)
BuildingValues = driver.find_element_by_id(BuildingId)
buildoptions = [x for x in BuildingValues.find_elements_by_tag_name("option")]
for b in buildoptions:
if b.get_attribute("value") is None or b.get_attribute("value") == '' or b.get_attribute("value") == ' ':
continue
print(b.get_attribute("value"))
----> # The Error Occurs here. <----
BuildingSelect = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, BuildingId)))
BuildingSelect.send_keys(b.get_attribute("value"))
# print(b.text)
except StaleElementReferenceException:
print('e')`