Python 如何防止过时的元素引用并在selenium中更新WebElement本身?

Python 如何防止过时的元素引用并在selenium中更新WebElement本身?,python,selenium,staleelementreferenceexception,Python,Selenium,Staleelementreferenceexception,我是一个处理python和selenium的新手,一周前刚刚开始,所以请原谅我的代码一团糟。我试图从中带有标记名的元素中提取所有“structure_id”和“d”信息,并将它们存储在单独的svg文件中。以下是我遇到问题的代码片段: for number in range(1,106): try: element = WebDriverWait(driver, 10).until( EC.presence_of_all_elements_locat

我是一个处理python和selenium的新手,一周前刚刚开始,所以请原谅我的代码一团糟。我试图从中带有标记名的元素中提取所有“structure_id”和“d”信息,并将它们存储在单独的svg文件中。以下是我遇到问题的代码片段:

for number in range(1,106):
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_all_elements_located((By.ID, 'master_group'))
        )
        selected = driver.find_element_by_class_name('simstripImgSel')

        driver.get(driver.current_url)
        paths = driver.find_elements_by_tag_name('path')
        
        for path in paths:
            while True:
                try:
                    structure = path.get_attribute('structure_id')
                    d = path.get_attribute('d')
                    break
                except Exception as e:
                    print(e)
                    paths = driver.find_elements_by_tag_name('path')
                    continue
            if structure != None:
                print('Attributes copied.')
                for word, initial in data.items():
                    structure = structure.replace(word,initial)
                filepath = Path('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg')
                if filepath.is_file():
                    text = open('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg','r+')
                    rep = text.read()
                    rep = rep.replace('</svg>','<path id="')
                    text.close()
                    os.remove('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg')
                    time.sleep(0.2)
                    text = open('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg','w+')
                    text.write(rep+str(structure)+'" d="'+str(d)+'"></path></svg>')
                    text.close()
                    print('File '+str(structure)+' modified in slice '+str(number)+'!')
                else:
                    svg = open('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg','w+')
                    svg.write('<svg id="the_svg_wrapper" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg"><path id="'+str(structure)+'" d="'+str(d)+'"></path></svg>')
                    svg.close()
                    print('File '+str(structure)+' made in slice '+str(number)+'!')

        selected.send_keys('F')
        paths = 0
        print()
        
    except Exception as e:
        print('Error.')
        print(e)
        break

print('Done!')
driver.quit()

范围内的数字(1106):
尝试:
元素=WebDriverWait(驱动程序,10)。直到(
EC.所有元素的存在((By.ID,“主组”))
)
selected=驱动程序。通过类名称(“simstripImgSel”)查找元素
driver.get(driver.current_url)
路径=驱动程序。通过标记名称(“路径”)查找元素
对于路径中的路径:
尽管如此:
尝试:
结构=路径。获取属性('structure\u id')
d=路径。获取属性('d')
打破
例外情况除外,如e:
打印(e)
路径=驱动程序。通过标记名称(“路径”)查找元素
持续
如果是结构!=无:
打印('已复制属性')
对于word,在data.items()中使用首字母:
结构=结构。替换(单词,首字母)
filepath=Path('C:\\Users\\justs\\Downloads\\Ordered\u svgs\\slice'+str(数字)+'\\'+str(结构)+'.svg')
如果filepath.is_file():
text=open('C:\\Users\\justs\\Downloads\\Ordered\u svg\\slice'+str(数字)+'\\'+str(结构)+'.svg',r+'))
rep=text.read()
rep=rep.replace(“”,“”)
text.close()
打印('File'+str(结构)+'modified in slice'+str(number)+'!'))
其他:
svg=open('C:\\Users\\justs\\Downloads\\Ordered\u svg\\slice'+str(数字)+'\\'+str(结构)+'.svg','w+'))
svg.write(“”)
svg.close()
打印('File'+str(结构)+'made in slice'+str(数字)+'!'))
已选择。发送_键('F'))
路径=0
打印()
例外情况除外,如e:
打印('错误')
打印(e)
打破
打印('Done!')
driver.quit()
这对于第一页很好,但我需要提取所有106页的路径,在按下“F”一次(进入下一页)后,我在
structure=path.get\u属性('structure\u id')
行得到一个陈旧的元素引用。起初,我认为加载路径需要一些时间,因此使用while循环,但到了第二页,它就被没完没了的陈旧元素引用卡住了


显式等待或刷新页面也不起作用,我怀疑
驱动程序。按\u class\u name查找\u element\u
WebElement根本没有更新(当我转到下一页后刷新页面时,我提取的文件最终与第一页相同,我在第5页得到了一个过时的元素引用)。我如何解决这个问题?感谢您的帮助

您循环了url,使其进入第1页

driver.get('http://atlas.brain-map.org/atlas?atlas=265297126#atlas=265297126&plate=102339919&structure=10155&x=42480&y=16378&zoom=-7&resolution=124.49&z=2')
for i in range(1,106):
    try:
        paths=WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.TAG_NAME, "path")))   
        for path in paths:
            structure = path.get_attribute('structure_id')
            d = path.get_attribute('d')
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "simstripImgSel"))).send_keys("F")
        time.sleep(0.5)
    except Exception as e:
        print(e)
进口

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

这成功了!在定义路径之前,我还添加了
time.sleep(5)
,以确保所有内容都已加载。非常感谢。