如何在Selenium中获取图像URI? [英] How to get image URI in Selenium?
本文介绍了如何在Selenium中获取图像URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对这个问题使用了第一个答案,以便使其适应我的需要:自动在笔记本电脑上保存指定网址的图片。
我的问题是如何获取网页上存在的每个图像的URI,以正确完成我的代码:
$ b
I used the first answer to this question in order to adapt it to my need: saving pictures of a given URL on my laptop automatically.
My problem is how to get the URI of every image that exist on the webpage in order to complete my code correctly:
import selenium
class TestFirefox:
def testFirefox(self):
self.driver=webdriver.Firefox()
# There are 2 pictures on google.com, I want to download them
self.driver.get("http://www.google.com")
self.l=[] # List to store URI to my images
self.r=self.driver.find_element_by_tag_name('img')
# I did print(self.r) but it does not reflect the URI of
# the image: which is what I want.
# What can I do to retrieve the URIs and run this:
self.l.append(self.image_uri)
for uri_to_img in self.l:
self.driver.get(uri_to_img)
# I want to download the images, but I am not sure
# if this is the good way to proceed since my list's content
# may not be correct for the moment
self.driver.save_screenshot(uri_to_image)
driver.close()
if __name__=='__main__':
TF=TestFirefox()
TF.testFirefox()
推荐答案
得到给定图像的 src
属性,以确定它的名称和(可能)地址 - 记住, src
也可以是相对URI。
You need to get get src
attribute of the given image in order to determine it's name and (possibly) address - remember, src
can be also relative URI.
for img in self.l:
url = img.get_attribute("src")
您应该尝试简单的HTTP客户端,比如 urllib
For downloading image you should try simple HTTP client like urllib
import urllib.request
urllib.request.urlretrieve(url, "image.png")
这篇关于如何在Selenium中获取图像URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文