简体   繁体   English

无法使用硒在python中获取图像src属性

[英]Unable to get image src attribute in python using selenium

When I try to get the src it returns None .当我尝试获取src时,它返回None The code should locate the image by its class name and get the attribute.代码应通过类名定位图像并获取属性。

import selenium.webdriver as webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()

url = ('https://www.instagram.com/cats')
driver.get(url)

time.sleep(3)

imgs = driver.find_element(By.CLASS_NAME, "_aagv").get_attribute("src")
print(imgs)

driver.quit()

在此处输入图像描述

I tired to use it with for loop as well but the results were same None .我也厌倦了将它与 for 循环一起使用,但结果是相同的None Any suggestions how to get it working?任何建议如何让它工作?

You can get the value of the src attribute using the below locator:您可以使用以下定位器获取 src 属性的值:

time.sleep(3)
images = driver.find_elements(By.CSS_SELECTOR, "._aagv img")

for image in images:
    print(image.get_attribute("src"))

The div class has the class that you are searching for ( "_aagv" ). div类具有您要搜索的类 ( "_aagv" )。 You need to get the inner image element ( <img> ), because it is the element that has the src tag on it.您需要获取内部图像元素 ( <img> ),因为它是带有src标签的元素。

You could either change your selector to match the Image tag directly, or you could select Image tags inside of the <div> tag you matched.您可以更改选择器以直接匹配 Image 标签,或者您可以在匹配的<div>标签内选择 Image 标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Selenium 无法获取 alt src 属性 - Python Selenium unable to get alt src attribute 使用 python 和 selenium 使用图像的“src”属性下载图像 - Using python and selenium to download an image using the image's “src” attribute 如何在selenium python中使用XPATH获取图像的src - how to get src of image using XPATH in selenium python 如何使用请求和 python 中的 beautifulsoup 从图像中获取 src 属性 - how to get src attribute from an image using requests and beautifulsoup in python 如何从中获取src属性 <image/> 用Python - How to get src attribute from <image/> with Python 使用Selenium Python获取Iframe Src内容 - Get Iframe Src content using Selenium Python Selenium Python get_attribute("src") 返回无,即使有一个“src”属性 - Selenium Python get_attribute("src") returns None, eventhough there is a "src" attribute 如何使用Selenium从iframe中的iframe获取属性src - How to get attribute src from iframe in iframe using Selenium 如何使用 selenium 获取 LinkedIn 多个配置文件的 src 属性 - how to get the src attribute of a LinkedIn multiple profiles using selenium 如何使用Python中的Selenium成功地将所有src图像嵌套在span标记下 - How to successfully get all src of image nested under span tag using Selenium in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM