0
0
Selenium Pythontesting~5 mins

XPath with text() in Selenium Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the XPath function text() do?
The text() function in XPath selects the text content inside an element. It helps locate elements by matching their visible text.
Click to reveal answer
beginner
Write an XPath to find a button with the exact text 'Submit'.
Use //button[text()='Submit']. This finds a button element whose text is exactly 'Submit'.
Click to reveal answer
intermediate
How do you find an element containing text 'Login' anywhere inside it using XPath?
Use //*[contains(text(), 'Login')]. This finds any element where the text includes 'Login' anywhere inside.
Click to reveal answer
beginner
Why is using text() in XPath useful for testing?
It helps find elements by their visible text, which is often stable and user-friendly. This makes tests easier to read and maintain.
Click to reveal answer
intermediate
What is a limitation of using text() in XPath?
If the element has nested tags or extra spaces, text() might not match exactly. You may need to use normalize-space() or other functions.
Click to reveal answer
Which XPath expression finds a div with exact text 'Hello World'?
A//div[@text='Hello World']
B//div[contains(text(),'Hello World')]
C//div[text()='Hello World']
D//div[text()*='Hello World']
What does //*[contains(text(),'Click')] select?
AAny element containing the word 'Click' anywhere in its text
BElements with attribute 'text' containing 'Click'
COnly <code>button</code> elements with text 'Click'
DAny element with text exactly 'Click'
Why might text() fail to match an element's visible text?
ABecause <code>text()</code> only matches attributes
BBecause the element has nested tags or extra spaces
CBecause <code>text()</code> is case-insensitive
DBecause <code>text()</code> matches only numbers
Which function helps remove extra spaces when using text() in XPath?
Astrip()
Btrim()
Cclean-text()
Dnormalize-space()
In Selenium Python, how do you find an element with text 'Home' using XPath?
Adriver.find_element(By.XPATH, "//*[text()='Home']")
Bdriver.find_element(By.ID, "Home")
Cdriver.find_element(By.XPATH, "//*[contains(text(),'Home')]", "")
Ddriver.find_element(By.CLASS_NAME, "Home")
Explain how the XPath text() function helps in locating elements during testing.
Think about how you find buttons or links by their labels.
You got /3 concepts.
    Describe a situation where using text() in XPath might not work as expected and how to fix it.
    Consider complex HTML structures or formatting.
    You got /3 concepts.