For secure links, href attribute value should have https
Language: Java
Framework: Selenium 4
1//Assumptions:2//1. The webpage to be tested has anchor tags with href attributes.3//2. The test case is to only test secure links (https).4//3. The Selenium 4 framework is already set up on the machine.56//Code to implement the test case in Java with Selenium 4:78import org.openqa.selenium.By;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.chrome.ChromeDriver;1213public class AnchorTagTest {14 public static void main(String[] args) {15 //Using local driver16 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");17 WebDriver driver = new ChromeDriver();1819 //Adding commented code to connect to remote client with desired capabilities20 /*21 DesiredCapabilities capabilities = new DesiredCapabilities();22 //Add desired capabilities here23 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);24 */2526 //Assuming the webpage URL to be tested27 String url = "https://www.example.com";2829 //Opening the webpage30 driver.get(url);3132 //Finding all anchor tags on the webpage33 List<WebElement> anchorTags = driver.findElements(By.tagName("a"));3435 //Iterating through the anchor tags36 for (WebElement anchorTag : anchorTags) {37 //Checking if the href attribute value starts with https38 String hrefValue = anchorTag.getAttribute("href");39 if (!hrefValue.startsWith("https")) {40 //If the href attribute value does not start with https, printing an error message41 System.out.println("Error: Non-secure link found - " + hrefValue);42 }43 }4445 //Closing the driver instance46 driver.quit();47 }48}
Language: Python
Framework: Selenium 4
1"""2Assumptions:3- The webpage has anchor tags that include href attributes.4- The href attributes' values may or may not be set to https links.5- The selenium webdriver module is installed.6- The webpage is accessible locally.7"""8# Import necessary modules9from selenium import webdriver1011# Connect to local driver12driver = webdriver.Firefox()1314# Navigate to webpage15driver.get("https://example.com")1617# Get all anchor tags18anchor_tags = driver.find_elements_by_tag_name("a")1920for anchor in anchor_tags:21 href = anchor.get_attribute("href")2223 # Check if href attribute value is https24 if href.startswith("https://"):25 print("Pass: " + href)26 else:27 print("Fail: " + href)2829# Disconnect from local driver30driver.quit()3132# Code to connect to remote client with desired capabilities33# ---------------------34# from selenium import webdriver35# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities3637# # Build desired capabilities38# capabilities = DesiredCapabilities.CHROME.copy()39# capabilities["version"] = "91.0.4472.19"40# capabilities["platform"] = "WINDOWS"4142# # Connect to remote client43# driver = webdriver.Remote(44# command_executor='http://localhost:4444/wd/hub',45# desired_capabilities=capabilities)4647# # Navigate to webpage and continue with script48# ---------------------
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