0
0
Selenium Pythontesting~10 mins

Find element by partial 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 part of its text. It then clicks the link and checks if the new page title is correct.

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 TestPartialLinkText(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_click_partial_link_text(self):
        driver = self.driver
        wait = WebDriverWait(driver, 10)
        # Wait until the link with partial text is present
        link = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'More information')))
        link.click()
        # Wait for the new page title to be as expected
        wait.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 is open at 'https://example.com' homepage-PASS
2Wait until link containing partial text 'More information' is presentPage loaded with visible link text 'More information...'Check presence of element with partial link text 'More information'PASS
3Click the found linkBrowser navigates to the linked page-PASS
4Wait until page title contains 'IANA'New page loaded with title containing 'IANA'Verify page title includes '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: The link with partial 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 by partial text?
Adriver.find_element(By.LINK_TEXT, 'More information')
Bdriver.find_element(By.PARTIAL_LINK_TEXT, 'More information')
Cdriver.find_element(By.ID, 'More information')
Ddriver.find_element(By.CLASS_NAME, 'More information')
Key Result
Use explicit waits with WebDriverWait and expected_conditions to reliably find elements by partial link text before interacting with them.