首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >图片库中15个元素后的StaleElementReferenceException

图片库中15个元素后的StaleElementReferenceException
EN

Stack Overflow用户
提问于 2019-05-04 09:58:50
回答 1查看 32关注 0票数 0

我正在尝试从我的网站上获得图片链接,它有大约40张图片,我正在使用driver.find_elements_by_css_selector将所有的图像都列到列表中。

当我循环遍历这个列表并打印这些图像的id时,对于前15个图像,它运行良好,然后抛出StaleElementReferenceException

我正在使用WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'image-list')))加载我的图片库。

我如何修复这个StaleElementReferenceException

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-04 14:44:11

我相信保存图像的容器会动态加载它们,所以您必须使用下面的逻辑。

代码语言:javascript
复制
# load the images in the dynamic list container
driver.find_element_by_xpath("(//*[@id='image-list']//img)[last()]").location_once_scrolled_into_view
time.sleep(1)
driver.find_element_by_xpath("(//*[@id='image-list']//img)[last()]").location_once_scrolled_into_view
time.sleep(1)
#get number of images
images = driver.find_elements_by_xpath("//*[@id='image-list']//img")
# use the for loop
for imageNum in range(len(images)):
    # access the image here
    image = driver.find_element_by_xpath("(//*[@id='image-list']//img[)" + str(imageNum+1) + "]")
    # now you can use the image element (first scroll to element)
    image.location_once_scrolled_into_view
    # get the image link here
    print(image.get_attribute('src'))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55981392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档