If a portion of the page is in a different language, use the lang attribute on that part.
Language: Java
Framework: Selenium 4
1//Assumptions:2//1. Webdriver is already installed on the local machine3//2. The website to be tested is already launched and the portion of the page that has different language is visible4//3. lang attribute is already specified on the portion of the page with different language5//4. The user can read the different language used on the page67import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;1112public class AccessibilityTesting {1314 public static void main(String[] args) {15 16 //Provide path to chromedriver.exe file on local machine17 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");18 19 //Create an instance of WebDriver for local use20 WebDriver driver = new ChromeDriver();21 22 //-------------------------------------23 //Code to connect to remote client with desired capabilities 2425 //Uncomment the code below and replace the hub URL and browser desired capabilities accordingly26 27 //DesiredCapabilities capabilities = DesiredCapabilities.chrome();28 //WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);29 //-------------------------------------30 31 //Launch the website to be tested in the browser32 driver.get("http://www.example.com");33 34 //Locate the portion of the page with different language using the lang attribute35 WebElement langSection = driver.findElement(By.xpath("//div[@lang='lang_code']"));36 37 //Check if the lang attribute is present on the element38 if(langSection.getAttribute("lang") != null) {39 System.out.println("Lang attribute present on the portion of the page with different language");40 }41 else {42 System.out.println("Lang attribute not present on the portion of the page with different language");43 }44 45 //Close the browser46 driver.quit();47 }4849}
Language: Python
Framework: Selenium 4
1# Assumptions: 2# 1. The application is a web page with language sections.3# 2. Selenium 4 is installed with required drivers.4# 3. Firefox browser is installed with latest version.56from selenium import webdriver7from selenium.webdriver.firefox.service import Service8from selenium.webdriver.firefox.options import Options910# Set up local driver to run tests on local machine11options = Options()12options.headless = True # Uncomment this line to run tests in headless mode13driver_path = "/path/to/geckodriver" # Update this path to geckodriver executable14driver = webdriver.Firefox(service=Service(driver_path), options=options)1516# Set up remote driver to run tests on remote client with desired capabilities17# Uncomment the following lines to connect to remote client18# capabilities = {19# "browserName": "firefox",20# "browserVersion": "latest",21# "platformName": "Linux",22# "acceptInsecureCerts": True23# }24# driver = webdriver.Remote(25# command_executor="http://localhost:4444/wd/hub",26# desired_capabilities=capabilities)2728# Navigate to application url29url = "https://www.example.com"30driver.get(url)3132# Check if all language sections have lang attribute33language_sections = driver.find_elements_by_css_selector("[lang]")34for section in language_sections:35 if not section.get_attribute("lang"):36 print("Language section is missing lang attribute")3738# Close browser window39driver.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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing