[英]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.