英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论