亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何防止過時的元素引用并使 WebElement 在 selenium 中自我更新?

如何防止過時的元素引用并使 WebElement 在 selenium 中自我更新?

慕后森 2023-10-26 10:47:36
我是一個處理 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()這對于第一頁效果很好,但我需要提取所有 106 頁的路徑,并且在按一次“F”后(移動到下一頁),我在 行 處獲得了陳舊的元素引用structure = path.get_attribute('structure_id')。最初,我認為路徑需要一些時間來加載,因此需要 while 循環,但到了第二頁,它就陷入了永無休止的陳舊元素引用。顯式等待或刷新頁面也不起作用,我懷疑 WebElementdriver.find_element_by_class_name根本沒有更新(當我進入下一頁后刷新頁面時,我提取的文件最終與第一頁相同,無論如何,我在第 5 頁得到了陳舊的元素引用)。我該如何解決這個問題?任何幫助表示贊賞!
查看完整描述

1 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

您循環了該 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


查看完整回答
反對 回復 2023-10-26
  • 1 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號