-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Update elements finders python docs #2099
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
Merged
diemol
merged 9 commits into
SeleniumHQ:trunk
from
pmartinez1:update-elements-finders-python-docs
Jul 20, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d2f612e
Add code that is to be used in examples
pmartinez1 fee6557
Updates to moving code for python Finders docs
pmartinez1 9e87d2c
Merge branch 'trunk' into update-elements-finders-python-docs
diemol d025197
Merge branch 'trunk' into update-elements-finders-python-docs
diemol 6d36b75
docs: temporarily keep inline Python for find-elements-from-element
diemol 49d18c5
Merge branch 'trunk' into update-elements-finders-python-docs
diemol c4c51f6
docs: use gh-codeblock for find-elements-from-element Python example
diemol 9ca4b34
fix: align Python finders examples with other language bindings
diemol 6eb6f9f
fix: address Qodo review feedback on Python finders examples
diemol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,94 @@ | ||
| import pytest | ||
| from selenium import webdriver | ||
| from selenium.webdriver.common.by import By | ||
|
|
||
| # The tests below marked as skipped mirror the HTML snippet shown at the top of the | ||
| # "Finding web elements" documentation and are illustrative only, matching how the | ||
| # same examples are shown for the other language bindings: | ||
| # | ||
| # <ol id="vegetables"> | ||
| # <li class="potatoes">… | ||
| # <li class="onions">… | ||
| # <li class="tomatoes"><span>Tomato is a Vegetable</span>… | ||
| # </ol> | ||
| # <ul id="fruits"> | ||
| # <li class="bananas">… | ||
| # <li class="apples">… | ||
| # <li class="tomatoes"><span>Tomato is a Fruit</span>… | ||
| # </ul> | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="illustrative example, not an executable test") | ||
|
diemol marked this conversation as resolved.
|
||
| def test_basic_finders(driver): | ||
| vegetable = driver.find_element(By.CLASS_NAME, 'tomatoes') | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="illustrative example, not an executable test") | ||
| def test_subset_of_dom(driver): | ||
| fruits = driver.find_element(By.ID, 'fruits') | ||
| fruit = fruits.find_element(By.CLASS_NAME, 'tomatoes') | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="illustrative example, not an executable test") | ||
| def test_optimized_locator(driver): | ||
| fruit = driver.find_element(By.CSS_SELECTOR, '#fruits .tomatoes') | ||
|
|
||
|
|
||
| @pytest.mark.skip(reason="illustrative example, not an executable test") | ||
| def test_all_matching_elements(driver): | ||
| plants = driver.find_elements(By.TAG_NAME, 'li') | ||
|
|
||
|
|
||
| def test_evaluating_shadow_dom(): | ||
| driver = webdriver.Chrome() | ||
| driver.implicitly_wait(5) | ||
| driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html') | ||
|
|
||
| shadow_host = driver.find_element(By.TAG_NAME, 'custom-checkbox-element') | ||
| shadow_root = shadow_host.shadow_root | ||
| assert shadow_root | ||
| shadow_content = shadow_root.find_element(By.CSS_SELECTOR, 'input[type=checkbox]') | ||
|
|
||
| assert shadow_host.is_displayed() | ||
| assert shadow_content.is_displayed() | ||
|
|
||
| driver.quit() | ||
|
|
||
|
|
||
| def test_get_element(): | ||
| driver = webdriver.Chrome() | ||
| driver.get('https://www.example.com') | ||
|
|
||
| elements = driver.find_elements(By.TAG_NAME, 'p') | ||
| for element in elements: | ||
| print(element.text) | ||
|
|
||
| assert len(elements) > 0 | ||
|
|
||
| driver.quit() | ||
|
|
||
|
|
||
| def test_find_elements_from_element(): | ||
| driver = webdriver.Chrome() | ||
| driver.get('https://www.example.com') | ||
|
|
||
| element = driver.find_element(By.TAG_NAME, 'div') | ||
| elements = element.find_elements(By.TAG_NAME, 'p') | ||
| for e in elements: | ||
| print(e.text) | ||
|
|
||
| assert len(elements) > 0 | ||
|
|
||
| driver.quit() | ||
|
|
||
|
|
||
| def test_get_active_element(): | ||
| driver = webdriver.Chrome() | ||
| driver.get('https://www.selenium.dev/selenium/web/web-form.html') | ||
|
|
||
| driver.find_element(By.CSS_SELECTOR, '[name="my-text"]').send_keys('webElement') | ||
| attr = driver.switch_to.active_element.get_attribute('name') | ||
|
|
||
| assert attr == 'my-text' | ||
|
|
||
| driver.quit() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.