0
0
Selenium Pythontesting~10 mins

Find element by link text in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page and finds a link using its visible text. It then clicks the link and verifies the new page title to confirm navigation.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestFindByLinkText(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_click_link_by_text(self):
        driver = self.driver
        # Wait until the link with text 'More information...' is present
        link = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.LINK_TEXT, 'More information...'))
        )
        link.click()
        # Wait for the new page title to contain 'IANA'
        WebDriverWait(driver, 10).until(EC.title_contains('IANA'))
        self.assertIn('IANA', driver.title)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window opens at https://example.com homepage-PASS
2Wait until link with text 'More information...' is presentPage loaded with visible link text 'More information...'Check presence of link element by link textPASS
3Click the link found by link textBrowser navigates to linked page-PASS
4Wait until page title contains 'IANA'New page loaded with title containing 'IANA'Verify page title contains 'IANA'PASS
5Assert that 'IANA' is in the page titlePage title is 'IANA — IANA-managed Reserved Domains' or similarassertIn('IANA', driver.title)PASS
6Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Link with text 'More information...' is not found within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What method is used to find the link element in this test?
Afind_element(By.CLASS_NAME, 'More information...')
Bfind_element(By.LINK_TEXT, 'More information...')
Cfind_element(By.ID, 'More information...')
Dfind_element(By.CSS_SELECTOR, 'a[href]')
Key Result
Use explicit waits to find elements by visible link text to ensure the element is present before interacting, improving test reliability.