Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
younicoin opened this issue Mar 13, 2025 · 1 comment

Comments

@younicoin
Copy link

younicoin commented Mar 13, 2025

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!

Copy link
Contributor

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant