Accessibility testing : Short alt text for images

If short alt text is sufficient to describe an image, provide the short text via the image's alt attribute.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the webpage is loaded in Chrome browser2/​/​Assuming there are images present on the web page3/​/​Assuming the Image tag has alt attribute4/​/​Connecting to remote client with desired capabilities like below56/​* DesiredCapabilities dc = new DesiredCapabilities();7dc.setCapability(CapabilityType.BROWSER_NAME, "chrome");8dc.setCapability(CapabilityType.VERSION, "91.0");9dc.setCapability("enableVNC", true);10dc.setCapability(CapabilityType.PLATFORM_NAME, Platform.LINUX);11RemoteWebDriver driver = new RemoteWebDriver(new URL("localhost:4444/​wd/​hub"), dc); */​1213import org.openqa.selenium.By;14import org.openqa.selenium.WebDriver;15import org.openqa.selenium.WebElement;16import org.openqa.selenium.chrome.ChromeDriver;1718public class AccessibilityTesting {1920public static void main(String[] args) {2122/​/​Initializing the chrome driver and loading the webpage23System.setProperty("webdriver.chrome.driver", "/​path/​to/​chromedriver");24WebDriver driver = new ChromeDriver();25driver.get("https:/​/​www.example.com");2627/​/​Finding all the image elements and checking the alt attribute28List<WebElement> images = driver.findElements(By.tagName("img"));29for(WebElement image : images) {30 String altText = image.getAttribute("alt");31 if(altText.isEmpty() || altText.isBlank()) {32 System.out.println("Image with no alt attribute: " + image.getAttribute("src"));33 } else if(altText.length() < 10) { /​/​Assuming short alt text is less than 10 characters34 System.out.println("Short alt text image: " + image.getAttribute("src"));35 }36}3738/​/​Closing the browser session39driver.quit();40}41}

Language: Python

Framework: Selenium 4

copy
1# Importing necessary libraries2from selenium import webdriver3from selenium.webdriver.common.keys import Keys45# Create new instance of Chrome Driver6driver = webdriver.Chrome()78# Remote client with desired capabilities uncomment this section of code and comment above 5 lines code9"""10from selenium import webdriver11from selenium.webdriver.common.keys import Keys12from selenium.webdriver.common.desired_capabilities import DesiredCapabilities1314# Set desired capabilities15capabilities = DesiredCapabilities.CHROME.copy()16capabilities['version'] = '91.0'1718# Connect to remote client19driver = webdriver.Remote(20 command_executor='http:/​/​0.0.0.0:4444/​wd/​hub',21 desired_capabilities=capabilities)22"""2324# Navigate to the webpage25driver.get("https:/​/​example.com")2627# Find all images on the page28images = driver.find_elements_by_tag_name('img')2930# Loop through each image and check the alt text31for image in images:32 alt_text = image.get_attribute('alt')33 if not alt_text or len(alt_text) < 10:34 print('Short alt text used for image with src:', image.get_attribute('src'))3536# Close the browser37driver.quit()

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing