提问人:hina abbasi 提问时间:4/20/2023 更新时间:4/20/2023 访问量:44
按类名查找元素 Selenium Python 对我不起作用
find elements by class name selenium python is not working for me
问:
我想获取具有相同类名的 Web 元素。我将使用它们来截取应用程序元素的屏幕截图。为此,我在 Python 中使用 Selenium。
url = "https://www.pexels.com/search/happy/"
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver.get(url)
elements = driver.find_elements(By.CLASS_NAME, "MediaCard_image__ljFAl")
print("**** Start *******")
print(elements.count)
for element in elements:
print("1")
print(element.text)
print("**** End *******")
输出为:
**** Start *******
<built-in method count of list object at 0x00000250202F2000>
1
1
1
1
1
1
1
1
**** End *******
我认为是空的,但为什么呢?有许多元素具有此类名。任何人都可以提供帮助吗?element.text
答:
2赞
Bibhav
4/20/2023
#1
您的选择者选择没有任何文本的图像。
您需要获取其属性,其中包含有关图像的文本:alt
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
url = "https://www.pexels.com/search/happy/"
driver = webdriver.Chrome()
driver.get(url)
elements = driver.find_elements(By.CLASS_NAME, "MediaCard_image__ljFAl")
print("**** Start *******")
for element in elements:
print(element.get_attribute("alt"))
print("**** End *******")
driver.quit() # Don't forget to quit drivers
输出:
**** Start *******
Free A Woman Lying on the Lap Stock Photo
Free Woman in White Crew Neck Shirt Holding Red and White Floral Heart Shaped Cake Stock Photo
Free Woman in Black Blazer Smiling With Mirror in the Table Stock Photo
Free Happy young female with long hair in warm turtleneck and leather jacket enjoying sunny day with closed eyes on street Stock Photo
Free People Looking at Laptop Stock Photo
Free Woman Wearing Brown Wicker Hat Stock Photo
Free Side View Photo of Smiling Woman Carrying Her Baby Stock Photo
Free Monochrome Photo of Woman Smiling Stock Photo
Free Monochrome Photography of Person Laughing Stock Photo
Free Young African American female keeping hands on head while laughing with closed eyes on white background Stock Photo
Free Baby in White Onesie Holding Wooden Blocks Stock Photo
Free Woman Wearing White Turtleneck Sweater Stock Photo
Free Happy black woman laughing on street Stock Photo
Free Photo Of Children In The Water Stock Photo
Free Photo Of Women Doing Their Skincare Stock Photo
Free Happy Father and Son Stock Photo
Free Happy Couple on Bed Stock Photo
Free Friends in Pink Clothes Stock Photo
Free Young cheerful female in dress looking at camera with toothy smile near pond and mount under cloudy sky Stock Photo
Free Black woman with dog near window Stock Photo
Free Photo Of Woman Wearing Pink Hijab Stock Photo
Free Woman Holding White Flower Bouquet Stock Photo
Free Woman Smiling Stock Photo
Free From above of African American smiling children with father lying on cozy bed and having fun while enjoying morning together on weekend Stock Photo
**** End *******
评论