Minimize the number of adjacent links to the same destination by combining adjacent images and text into a single link, rather than creating a separate link for each element.
Language: Java
Framework: Selenium 4
1// Assuming we are testing a webpage that contains multiple elements that lead to the same destination2// We will use Selenium 4 framework with Java to automate the accessibility testing34import org.openqa.selenium.*;5import org.openqa.selenium.chrome.ChromeDriver;67public class AccessibilityTest {89 public static void main(String[] args) {1011 // Enter the path of the chrome driver executable12 System.setProperty("webdriver.chrome.driver", "/path/to/chrome/driver");1314 // Create a new instance of the Chrome driver15 WebDriver driver = new ChromeDriver();1617 // Navigate to the webpage18 driver.get("https://example.com/accessibility");1920 // Find all the links on the webpage21 List<WebElement> links = driver.findElements(By.tagName("a"));2223 // Create a new arraylist to store the combined links24 List<String> combinedLinks = new ArrayList<String>();2526 // Iterate through all the links on the webpage27 for (WebElement link : links) {2829 // Get the href attribute value of the link30 String href = link.getAttribute("href");3132 // Check if the href attribute value is not null and not empty33 if (href != null && !href.isEmpty()) {3435 // Check if the href attribute value is already present in the combined links arraylist36 if (!combinedLinks.contains(href)) {3738 // Add the href attribute value to the combined links arraylist39 combinedLinks.add(href);4041 // Combine adjacent links with the same destination42 for (WebElement adjacentLink : links) {4344 // Check if the adjacent link has the same href attribute value as the current link45 if (adjacentLink.getAttribute("href").equals(href)) {4647 // Combine the text and image of the adjacent link with the current link48 WebElement combinedLink = driver.findElement(By.partialLinkText(adjacentLink.getText()));49 link = link.findElement(By.tagName("img"));50 combinedLink.click();51 }52 }53 }54 }55 }5657 // Close the browser58 driver.quit();59 }60} 6162// To connect to remote client with desired capabilities, we can use the following code:6364import org.openqa.selenium.*;65import org.openqa.selenium.remote.*;6667public class AccessibilityTest {6869 public static void main(String[] args) {7071 // Create a new instance of the RemoteWebDriver72 DesiredCapabilities capabilities = DesiredCapabilities.chrome();73 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);7475 // Rest of the code remains the same as before7677 }78}
Language: Python
Framework: Selenium 4
1from selenium import webdriver2from selenium.webdriver.common.by import By3from selenium.webdriver.support.ui import WebDriverWait4from selenium.webdriver.support import expected_conditions as EC56#Assumptions:7#1. The web application is built using HTML, CSS and JavaScript.8#2. The application has multiple links with the same destination.9#3. The application has no accessibility issues.1011#Code to use local driver12driver = webdriver.Chrome()13#Code to connect to remote client with desired capabilities (uncomment if needed)14#desired_caps = {'platform': 'MAC', 'browserName': 'chrome', 'version': '91.0'}15#driver = webdriver.Remote(command_executor='http://<REMOTE CLIENT IP>/wd/hub', desired_capabilities= desired_caps)1617# Open the web application URL18driver.get("https://www.example.com")1920# Wait for all elements to load21wait = WebDriverWait(driver, 10)22wait.until(EC.presence_of_all_elements_located)2324# Find all the links with the same destination25same_destination_links = driver.find_elements(By.XPATH, "//a[@href='/destination']")2627# Get the first link element28first_link = same_destination_links[0]2930# Iterate through all the links and click the first one31for link in same_destination_links:32 link_text = link.text33 if link_text != '':34 first_link = link35 break3637# Minimize the number of adjacent links to the same destination by combining adjacent images and text into a single link38for link in same_destination_links:39 if link.text != '' and link.text != first_link.text:40 # click on the link to be combined with the first link41 link.click()42 # merge the two links with the same destination43 driver.execute_script("return arguments[0].appendChild(arguments[1]);", first_link, link)4445# Assert that the number of links with the same destination is minimized46assert len(driver.find_elements(By.XPATH, "//a[@href='/destination']")) == 14748# Close the browser49driver.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