All the anchor tags should have url value for href attribute
Language: Java
Framework: Selenium 4
1// Assumption: The web page under test has valid and legitimate anchor tags.2// The web page has loaded successfully.3// The browser driver has been initialized and is ready for use.45import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;910public class AnchorTagTesting {11 public static void main(String[] args) {12 13 // Initialize the driver for Chrome browser 14 System.setProperty("webdriver.chrome.driver", "C:/path/to/chromedriver.exe"); 15 WebDriver driver = new ChromeDriver();1617 // Navigate to webpage under test18 driver.get("https://www.example.com");1920 // Get all the anchor tags in the webpage21 List<WebElement> allLinks = driver.findElements(By.tagName("a"));2223 // Loop through the anchor tags and check for href attribute value24 for (WebElement link : allLinks) {25 String href = link.getAttribute("href");26 if (href == null || href.isEmpty()) {27 // Report failure - href attribute value is missing28 System.out.println("TEST FAILED - Anchor tag missing href attribute value: " + link.getText());29 } else {30 // Report success - href attribute value is present31 System.out.println("TEST PASSED - Anchor tag has href attribute value: " + link.getText() + " -> " + href);32 }33 }3435 // close the browser window36 driver.quit();37 }38 39 // Drivers are not required in remote clients since runner program does not need to interact with the screen40 // Commented code to connect to remote client with desired capabilities:41 /*42 public static void main(String[] args) throws MalformedURLException {43 DesiredCapabilities capabilities = new DesiredCapabilities();44 capabilities.setBrowserName("chrome");45 //Add other capabilities as per the requirements46 WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.12:5566/wd/hub"), capabilities);47 // rest of the code remains same as above48 }49 */50}
Language: Python
Framework: Selenium 4
1from selenium.webdriver import Chrome2from selenium.webdriver.common.by import By3from selenium.webdriver.support.ui import WebDriverWait4from selenium.webdriver.support import expected_conditions as EC56# Assuming the webpage we want to test is loaded in a local driver7driver = Chrome()89# If we want to connect to a remote client with desired capabilities, we can use the following commented code10"""11from selenium.webdriver import Remote1213capabilities = {'browserName': 'chrome'}14driver = Remote(command_executor='http://127.0.0.1:4444/wd/hub',15 desired_capabilities=capabilities)16"""1718# Navigate to the webpage we want to test19driver.get('https://www.example.com')2021# Find all anchor tags in webpage22anchor_tags = driver.find_elements(By.TAG_NAME, 'a')2324# Loop through each anchor tag and check if 'href' attribute has a value25for tag in anchor_tags:26 href_value = tag.get_attribute('href')27 assert href_value, f'No href value found for {tag.get_attribute("innerHTML")}'2829driver.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