seleniumhq.github.io icon indicating copy to clipboard operation
seleniumhq.github.io copied to clipboard

[🚀 Feature]: Documentation: correct code of "get children of tag 'ul' with tag 'li'"

Open younicoin opened this issue 9 months ago • 1 comments

Hello, there is a tiny mistake on page /documentation/webdriver/elements/finders/

    # Get first element of tag 'ul'
element = driver.find_element(By.XPATH, '//ul')

    # get children of tag 'ul' with tag 'li'
elements  = driver.find_elements(By.XPATH, './/li')

# actually should be, if you really need children
elements  = element.find_elements(By.XPATH, './/li')

Here it is my example demonstrating mistake:

#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.selenium.dev/selenium/web/draggableLists.html")
element = driver.find_element(By.XPATH, "//ul[2]")
print ("  Correct way: use element")
elements = element.find_elements(By.XPATH, ".//li")
for e in elements:
    print(e.text)

print ("\n  Incorrect way: use driver")
elements2 = driver.find_elements(By.XPATH, ".//li")
for e2 in elements2:
    print(e2.text)

driver.quit()

It gives ./subelements.py Correct way: use element RightItem 1 RightItem 2 RightItem 3 RightItem 4 RightItem 5

Incorrect way: use driver LeftItem 1 LeftItem 2 LeftItem 3 LeftItem 4 LeftItem 5 RightItem 1 RightItem 2 RightItem 3 RightItem 4 RightItem 5

So, 'driver' is top-level object relating to whole web-page. But if you need children li elements of ul, you need to use 'element' instead of 'driver'.

PS: for your html code provided on that page of documentation that works, because there is only one tag ul. But that page, that I used, tere are two ul's, so for certain relation need to use that 'element' you got on first step, instead of 'driver'. Thank you!

younicoin avatar Mar 13 '25 16:03 younicoin

@younicoin, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] avatar Mar 13 '25 16:03 github-actions[bot]

Thanks for raising this @younicoin , #2471 fixes it.

rpallavisharma avatar Sep 05 '25 11:09 rpallavisharma