Micky Rajkumar
Micky Rajkumar

Reputation: 1

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

I am trying to test my website using selenium. And I want to check all images have filled alt attribute or not. So how to check this.

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

Upvotes: 0

Views: 398

Answers (1)

kite
kite

Reputation: 541

I'm not well versed with selenium, but this can be done easily using requests with bs4 (simple web-scraping).

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)

Upvotes: 1

Related Questions