简体   繁体   English

如何使用硒读取html页面中的所有图像alt?

[英]how to read all images alt in html page using selenium?

I am trying to test my website using selenium.我正在尝试使用 selenium 测试我的网站。 And I want to check all images have filled alt attribute or not.我想检查所有图像是否填充了 alt 属性。 So how to check this.那么如何检查这个。

<img src="/media/images/biren.png" alt="N. Biren" class ="img-fluid" >

I'm not well versed with selenium, but this can be done easily using requests with bs4 (simple web-scraping).我不太熟悉 selenium,但这可以通过bs4 (简单的网络抓取)使用requests轻松完成。

Please find an example code below:请在下面找到示例代码:

import requests, bs4

url = 'HTML URL HERE!'
# get the url html txt
page = requests.get(url).text
# parse the html txt
soup = bs4.BeautifulSoup(page, 'html.parser')

# get all img tags
for image in soup.find_all('img'):
    try:
        # print alt text if exists
        print(image['alt'])
    except:
        # print the complete img tag if not
        print(image)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM