我正在尝试从我的网站上获得图片链接,它有大约40张图片,我正在使用driver.find_elements_by_css_selector将所有的图像都列到列表中。
当我循环遍历这个列表并打印这些图像的id时,对于前15个图像,它运行良好,然后抛出StaleElementReferenceException
我正在使用WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'image-list')))加载我的图片库。
我如何修复这个StaleElementReferenceException
谢谢。
发布于 2019-05-04 14:44:11
我相信保存图像的容器会动态加载它们,所以您必须使用下面的逻辑。
# 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'))https://stackoverflow.com/questions/55981392
复制相似问题