Python selenium获取图像源属性时返回”none”。

huangapple go评论72阅读模式
英文:

Python selenium saying none when getting attribute for source of image

问题

我正在尝试使用Selenium获取类KfFlO中图像的src我对这个相当陌生),但似乎没有显示并且当我要求打印源时Python会一直打印none”。有人知道问题是什么吗我的代码如下

英文:

I'm trying to get the src of the images in the class "KfFlO" using selenium(I am fairly new to this) however it does not seem to appear and python keeps printing "none" when I ask to print for the source. Does anyone know what the problem is? My code is listed below

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

url_muse="https://musescore.com/user/26033606/scores/6273879"

browser=webdriver.Chrome()
browser.get(url_muse)

image_elements = browser.find_elements(By.CLASS_NAME,"KfFlO")

src=[]
for my_href in image_elements:
    src.append(my_href.get_attribute("href"))
print(src)

Thanks-Jake超新星

答案1

得分: 1

处理动态元素时请使用WebDriverWait()等待visibility_of_all_elements_located()要获取`src`,需要使用`get_attribute("src")`

    url_muse = "https://musescore.com/user/26033606/scores/6273879"
    
    browser = webdriver.Chrome()
    browser.get(url_muse)
    print([image.get_attribute("src") for image in WebDriverWait(browser, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "img.KfFlO")))])

需要导入以下库

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
英文:

To handle dynamic element use WebDriverWait() and wait for visibility_of_all_elements_located() and to get the src you need to use get_attribute("src")

url_muse="https://musescore.com/user/26033606/scores/6273879"

browser=webdriver.Chrome()
browser.get(url_muse)
print([image.get_attribute("src") for image in WebDriverWait(browser,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"img.KfFlO")))])

You need to import below libraries

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

huangapple
  • 本文由 发表于 2023年2月27日 15:34:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定