Accessibility testing : Avoid emulating links and buttons

Avoid emulating links and buttons. Use the a and button tags appropriately. Avoid using a tags for buttons. Avoid using div, span, etc. tags for links or buttons.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming that the webpage is built with HTML and uses the a and button tags for links and buttons properly2/​/​This test is checking if the website is following accessibility standards by using the a and button tags appropriately34import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;89public class AccessibilityTesting {1011 public static void main(String[] args) {1213 /​/​ Setting up the local driver14 System.setProperty("webdriver.chrome.driver", "/​path/​to/​chromedriver");15 WebDriver driver = new ChromeDriver();1617 /​/​ Using the driver to navigate to the webpage18 driver.get("https:/​/​www.example.com");1920 /​/​ Checking if links use the a tag21 WebElement link = driver.findElement(By.xpath("/​/​a[text()='Example Link']"));22 if (link != null) {23 System.out.println("Link is using a tag");24 } else {25 System.out.println("Link is not using a tag");26 }2728 /​/​ Checking if buttons use the button tag29 WebElement button = driver.findElement(By.xpath("/​/​button[text()='Example Button']"));30 if (button != null) {31 System.out.println("Button is using button tag");32 } else {33 System.out.println("Button is not using button tag");34 }3536 /​/​Checking if any other element is used as a link/​button37 WebElement otherElement = driver.findElement(By.xpath("/​/​div[text()='Example Link']"));38 if (otherElement != null) {39 System.out.println("Other element used as link/​button");40 } else {41 System.out.println("Other element not used as link/​button");42 }4344 /​/​Closing the browser45 driver.quit();46 }47 48 /​/​Code to connect to remote client with desired capabilities49 /​/​Assuming that the remote client already has the necessary desired capabilities set up50 public WebDriver connectToRemoteClient() {51 WebDriver driver = null;52 try {53 String hubUrl = "http:/​/​<remote-ip>:4444/​wd/​hub";54 DesiredCapabilities capabilities = DesiredCapabilities.chrome();55 driver = new RemoteWebDriver(new URL(hubUrl), capabilities);56 System.out.println("Connected to remote client successfully");57 } catch (MalformedURLException e) {58 System.out.println("Error connecting to remote client: " + e.getMessage());59 }60 return driver;61 }6263}

Language: Python

Framework: Selenium 4

copy
1#Assuming the web application's page contains different types of links and buttons2#and we want to test accessibility by using appropriate tags for links and buttons34from selenium import webdriver56#local driver7driver = webdriver.Chrome()8#remote client with desired capabilities commented out9'''10from selenium.webdriver.common.desired_capabilities import DesiredCapabilities11from selenium import webdriver1213capabilities = DesiredCapabilities.CHROME.copy()14capabilities['platform'] = "WINDOWS"15capabilities['version'] = "10"16capabilities['screenResolution'] = "1280x720"1718driver = webdriver.Remote(19 command_executor='http:/​/​<INSERT_IP_ADDRESS>:<INSERT_PORT>/​wd/​hub',20 desired_capabilities=capabilities)21'''2223#Navigate to the web application page24driver.get("http:/​/​www.example.com")2526#Use a tags for links27link_elements = driver.find_elements_by_tag_name("a")28for link_element in link_elements:29 is_button = link_element.get_attribute("role") == "button"30 if not is_button:31 print("Link: ", link_element.text, " uses appropriate tag.")3233#Use button tags for buttons34button_elements = driver.find_elements_by_tag_name("button")35for button_element in button_elements:36 print("Button: ", button_element.text, " uses appropriate tag.")3738#Avoid using a tags for buttons and div, span etc tags for links or buttons3940#find a tags with role attribute equal to button41wrong_button_elements = driver.find_elements_by_xpath("/​/​a[@role='button']")42for wrong_button_element in wrong_button_elements:43 print("A tag used for button: ", wrong_button_element.text, " instead of button tag.")4445#find divs and spans with role attribute equal to link46wrong_link_elements = driver.find_elements_by_xpath("/​/​div[@role='link']|/​/​span[@role='link']")47for wrong_link_element in wrong_link_elements:48 print("Div or span tag used for link: ", wrong_link_element.text, " instead of a tag.")

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