0
0
Selenium Pythontesting~10 mins

Absolute vs relative XPath in Selenium Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find an element using an absolute XPath.

Selenium Python
element = driver.find_element(By.XPATH, '[1]')
Drag options to blanks, or click blank then click option'
A//div[@id='main']
B/html/body/div[1]/header/nav
C//*[@class='button']
D//input[@name='search']
Attempts:
3 left
💡 Hint
Common Mistakes
Using relative XPath when absolute is required
Starting XPath with '//' for absolute path
2fill in blank
medium

Complete the code to find an element using a relative XPath.

Selenium Python
element = driver.find_element(By.XPATH, '[1]')
Drag options to blanks, or click blank then click option'
A//div[@class='content']
B/html/body/div[2]/section
C/html/body/footer
D/html/head/title
Attempts:
3 left
💡 Hint
Common Mistakes
Using absolute XPath when relative is expected
Missing the double slashes at the start
3fill in blank
hard

Fix the error in the XPath expression to select a button with class 'submit'.

Selenium Python
button = driver.find_element(By.XPATH, '[1]')
Drag options to blanks, or click blank then click option'
A/button[@class='submit']
B//button[class='submit']
Cbutton[@class='submit']
D//button[@class='submit']
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting '@' before attribute name
Using single slash for relative XPath
4fill in blank
hard

Fill both blanks to create a relative XPath that selects all <li> elements inside a <ul> with id 'menu'.

Selenium Python
items = driver.find_elements(By.XPATH, '[1]/li[2]')
Drag options to blanks, or click blank then click option'
A[contains(@class, 'item')]
B//div[@id='menu']
C//ul[@id='menu']
D[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong tag name in XPath
Adding unnecessary predicates
5fill in blank
hard

Fill all three blanks to create a relative XPath that selects the first <input> element with type 'text' inside a form with class 'login'.

Selenium Python
input_element = driver.find_element(By.XPATH, '[1][2][3]')
Drag options to blanks, or click blank then click option'
A//form[contains(@class, 'login')]//input
B[@type='text']
C[1]
D//div[@class='login']//input
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong tag or attribute
Missing the index to select the first element