Reserve tables for tabular data, use table headers appropriately, and use table captions.
Language: Java
Framework: Selenium 4
1//Assuming the webpage has a table with headers and captions2//Using Chrome as the local driver3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.By;6import org.openqa.selenium.WebElement;7import java.util.List;89public class AccessibilityTesting {1011 public static void main(String[] args) {12 System.setProperty("webdriver.chrome.driver", "/path/to/chrome/driver");13 WebDriver driver = new ChromeDriver();14 try {15 driver.get("http://example.com");16 WebElement table = driver.findElement(By.tagName("table"));17 List<WebElement> headers = table.findElements(By.tagName("th"));18 List<WebElement> captions = table.findElements(By.tagName("caption"));1920 //Checking if table has headers21 if(headers.size() == 0) {22 System.out.println("Table has no headers");23 }24 else {25 System.out.println("Table headers: ");26 for (WebElement header : headers) {27 System.out.println(header.getText());28 }29 }3031 //Checking if table has captions32 if(captions.size() == 0) {33 System.out.println("Table has no captions");34 }35 else {36 System.out.println("Table captions: ");37 for (WebElement caption : captions) {38 System.out.println(caption.getText());39 }40 }41 }42 finally {43 driver.quit();44 }45 }46 47 //Uncomment the following code to connect to remote client with desired capabilities48 /*49 import org.openqa.selenium.remote.DesiredCapabilities;50 import org.openqa.selenium.remote.RemoteWebDriver;51 import java.net.URL;52 53 public static void main(String[] args) {54 DesiredCapabilities capabilities = DesiredCapabilities.chrome();55 capabilities.setCapability("version", "latest");56 capabilities.setCapability("platform", "WIN10");57 58 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);59 try {60 driver.get("http://example.com");61 WebElement table = driver.findElement(By.tagName("table"));62 List<WebElement> headers = table.findElements(By.tagName("th"));63 List<WebElement> captions = table.findElements(By.tagName("caption"));6465 //Checking if table has headers66 if(headers.size() == 0) {67 System.out.println("Table has no headers");68 }69 else {70 System.out.println("Table headers: ");71 for (WebElement header : headers) {72 System.out.println(header.getText());73 }74 }7576 //Checking if table has captions77 if(captions.size() == 0) {78 System.out.println("Table has no captions");79 }80 else {81 System.out.println("Table captions: ");82 for (WebElement caption : captions) {83 System.out.println(caption.getText());84 }85 }86 }87 finally {88 driver.quit();89 }90 }91 */92}
Language: Python
Framework: Selenium 4
1# Assumptions:2# 1. The Selenium package is already installed.3# 2. The Chrome driver is located in the project directory.4# 3. The web application being tested contains tables with headers and captions.56# Import required packages7from selenium import webdriver8from selenium.webdriver.common.keys import Keys910# Set the desired capabilities to connect to a remote client11# desired_capabilities = {12# "browserName": "chrome",13# "browserVersion": "latest",14# "platformName": "Windows 10",15# "seleniumVersion": "3.14.0"16# }1718# Instantiate the web driver19driver = webdriver.Chrome()2021# Navigate to the web application to be tested22driver.get("https://www.example.com/")2324# Find the tables in the web page25tables = driver.find_elements_by_tag_name("table")2627# Iterate through the tables28for table in tables:29 # Check if each table has a caption30 caption = table.find_elements_by_tag_name("caption")31 assert caption, "Table is missing a caption"32 33 # Check if each table has a header row34 headers = table.find_elements_by_tag_name("thead")35 assert headers, "Table is missing a header row"36 37 # Check if each header row has at least one header cell38 header_cells = headers[0].find_elements_by_tag_name("th")39 assert header_cells, "Header row is missing header cells"40 41 # Check if each table row has at least one data cell42 rows = table.find_elements_by_tag_name("tr")43 for row in rows:44 data_cells = row.find_elements_by_tag_name("td")45 assert data_cells, "Table row is missing data cells"46 47# Close the web driver48driver.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