Provide a link to skip to the main content as the first focusable link on the page.
Language: Java
Framework: Selenium 4
1//Assuming that the web page is using HTML structure with anchor tag (<a>) for the link to skip to main content2//Assuming that the link has id "skip-link" to allow for easy access in the code34import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;89public class SkipToMainContentTest {10 public static void main(String[] args) throws InterruptedException {11 12 //Assuming that the driver is saved locally and has been added to the system PATH variable13 WebDriver driver = new ChromeDriver(); 14 15 //Assuming that the web page URL is known and can be navigated to16 driver.get("https://example.com/"); 17 18 //Assuming that the link to skip to main content is coded with anchor tag and has id "skip-link"19 WebElement skipLink = driver.findElement(By.id("skip-link")); 20 String skipLinkHref = skipLink.getAttribute("href");21 22 //Assuming that clicking the skip link would lead to the main content of the page23 skipLink.click(); 24 25 //Assuming that the main content of the page has unique identifier and can be checked26 Thread.sleep(2000); //Waiting for the page to load completely27 WebElement mainContent = driver.findElement(By.id("main-content"));28 String mainContentText = mainContent.getText();29 30 //Asserting that the main content is now in focus, assuming that the text is displayed31 assert(!mainContentText.isEmpty()); 32 33 //Assuming that the code is used in a remote client with desired chrome capabilities34 //DesiredCapabilities capabilities = DesiredCapabilities.chrome();35 //WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);36 37 //Quitting the driver once the test is completed38 driver.quit(); 39 }40}
Language: Python
Framework: Selenium 4
1from selenium import webdriver23# assuming website URL is "https://example.com"4url = "https://example.com"56# assuming there is a button on the top of the page named "Skip to main content"7skip_to_main_content_btn = "Skip to main content"89# assuming the id of the main content section is "main-content"10main_content_id = "main-content"1112# assuming the browser is Chrome and the webdriver is saved locally13driver = webdriver.Chrome()1415# accessing the website URL16driver.get(url)1718# searching for the "Skip to main content" button19skip_to_main_content = driver.find_element_by_link_text(skip_to_main_content_btn)2021# verifying the link is the first focusable link22assert skip_to_main_content == driver.switch_to.active_element2324# assuming the test needs to be run on a remote client with desired capabilities25from selenium.webdriver.common.desired_capabilities import DesiredCapabilities2627# assuming the desired browser is Firefox and desired version is 8028desired_cap = {'browserName': 'firefox', 'browserVersion': '80.0'}2930# connecting to remote client with desired capabilities31remote_url = "http://localhost:4444/wd/hub"32driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=desired_cap)
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