Accessibility testing : lang attribute for different language sections

Provide a lang attribute on the page's HTML element. When a visual label is present for an interactive element (e.g., link or form control), the accessible name of the element should contain the visual label.

Language: Java

Framework: Selenium 4

copy
1/​/​ Open browser and navigate to web page2WebDriver driver = new ChromeDriver();3driver.get("https:/​/​www.example.com");45/​/​ Set lang attribute on HTML element6WebElement html = driver.findElement(By.tagName("html"));7html.setAttribute("lang", "en");89/​/​ Check visual label for interactive element10WebElement link = driver.findElement(By.linkText("Example Link"));11String label = link.getText();12String accessibleName = link.getAttribute("aria-label");13if(!accessibleName.equals(label)) {14 System.out.println("Error: Accessible name does not match visual label.");15}1617/​/​ Connect to remote client with desired capabilities18DesiredCapabilities capabilities = new DesiredCapabilities();19capabilities.setBrowserName("chrome");20capabilities.setVersion("latest");21capabilities.setCapability("platform", "Windows 10");22capabilities.setCapability("screenResolution", "1280x720");2324WebDriver remoteDriver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver2from selenium.webdriver.common.desired_capabilities import DesiredCapabilities34# Assuming the webpage has different language sections5# and each section is tagged with a div element having lang attribute6# We will use the local Chrome browser for testing, but the code can also connect to remote client7# using desired capabilities mentioned below89# desired_capabilities = DesiredCapabilities.CHROME.copy()10# desired_capabilities['platform'] = "WINDOWS|MAC"11# driver = webdriver.Remote(command_executor='http:/​/​localhost:4444/​wd/​hub', desired_capabilities=desired_capabilities)1213driver = webdriver.Chrome()1415# Load the webpage to test16driver.get("https:/​/​example.com")1718# Find all the div elements containing lang attribute19sections = driver.find_elements_by_css_selector('div[lang]')2021# Iterate through each section and check if it has a visual label for interactive elements22for section in sections:23 # Get all the interactive elements in this section24 interactive_elements = section.find_elements_by_css_selector('a, button, input[type="button"], input[type="submit"]')25 26 for element in interactive_elements:27 # Get the accessible name of the element28 accessible_name = element.get_attribute('aria-label')29 # Get the visual label of the element30 visual_label = element.text31 32 if visual_label:33 # If element has a visual label, ensure its accessible name contains the visual label34 assert visual_label in accessible_name, f"Label {visual_label} not found in accessible name {accessible_name}" 35 36# Close the browser window37driver.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