The purpose of each link can be determined from the link text alone, or from the link text and the containing paragraph, list item, or table cell, or the link text and the title attribute.
Language: Java
Framework: Selenium 4
1//Assuming the webpage uses HTML tags23import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;78public class LinkPurposeDeterminableTest {910 public static void main(String[] args) {11 12 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");13 WebDriver driver = new ChromeDriver();14 15 //Connecting to remote client with desired capabilities16 /*17 * DesiredCapabilities capabilities = new DesiredCapabilities();18 * capabilities.setCapability();19 * RemoteWebDriver driver = new RemoteWebDriver(new URL(""),capabilities);20 */21 22 //Assuming the website URL is https://www.example.com23 driver.get("https://www.example.com");24 25 //Finding all the links on the webpage26 List<WebElement> links = driver.findElements(By.tagName("a"));27 28 //Iterating through the links to check for link purpose determinability29 for(WebElement link: links) {30 31 String linkText = link.getText();32 String linkTitle = link.getAttribute("title");33 String linkParentText = link.findElement(By.xpath("..")).getText(); //assuming the links are contained in HTML tags34 35 if(linkTitle != null && !linkTitle.isEmpty()) {36 //Checking if the link text and title attribute provide sufficient information37 System.out.println("Link purpose is determinable using link text and title attribute for link " +linkText);38 }39 40 else if(linkParentText != null && !linkParentText.isEmpty()) {41 //Checking if the link text and its parent element provide sufficient information42 System.out.println("Link purpose is determinable using link text and its parent element for link " +linkText);43 }44 45 else {46 System.out.println("Link purpose is NOT determinable for link " +linkText);47 }48 }49 50 driver.quit();51 }5253}
Language: Python
Framework: Selenium 4
1# These are the assumptions made about the application and environment:2# 1. The website being tested has links visible to the user.3# 2. The link text is unique and descriptive.4# 3. The link text is not obfuscated.5# 4. The link text is in English.6# 5. The links are not dynamically generated.78from selenium.webdriver import Chrome9from selenium.webdriver.common.by import By10from selenium.webdriver.support.ui import WebDriverWait11from selenium.webdriver.support import expected_conditions as EC1213# Set up local driver14driver = Chrome()1516# Set desired capabilities for remote client17# desired_caps = {18# 'browserName': 'chrome',19# 'version': '91.0',20# 'platform': 'WINDOWS',21# 'name': 'Accessibility testing - Link purpose determinable from link text',22# 'build': 'Build Number 123'23# }24# driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities=desired_caps)2526# Navigate to the website being tested27driver.get('https://www.example.com')2829# Wait for all links to be loaded30wait = WebDriverWait(driver, 10)31wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'a')))3233# Loop through all links on the page34for link in driver.find_elements_by_tag_name('a'):35 36 # Get the link text and title attribute37 link_text = link.text38 link_title = link.get_attribute('title')39 40 # Get the containing element's text41 containing_text = link.find_element_by_xpath('./..').text42 43 # Determine if the link purpose is determinable from link text alone or in combination with containing element's text or title attribute44 if link_text.strip() == '':45 print('Link text is empty')46 elif len(link_text) > 64:47 print('Link text is too long')48 elif '[image]' in link_text:49 print('Link text contains image description')50 elif '[video]' in link_text:51 print('Link text contains video description')52 elif containing_text.strip() != '' and link_text.lower() not in containing_text.lower():53 print('Link text does not appear in the containing element\'s text')54 elif link_title is not None and link_text.lower() not in link_title.lower():55 print('Link text does not appear in the title attribute')56 else:57 print('Link purpose is determinable from link text or in combination with containing element\'s text or title attribute')5859# Close the browser window60driver.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