Skip to content

Commit d2f612e

Browse files
committed
Add code that is to be used in examples
Moving the code to the appropriate files
1 parent ff1ddd7 commit d2f612e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Diff for: examples/python/tests/elements/test_finders.py

+94
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,96 @@
11
from selenium import webdriver
2+
from selenium.webdriver.common.by import By
3+
4+
5+
def test_basic_finders():
6+
driver = webdriver.Chrome()
7+
driver.get('https://www.selenium.dev/')
8+
9+
body_on_page = driver.find_element(By.CLASS_NAME, 'td-home')
10+
container_on_page = body_on_page.find_element(By.CLASS_NAME, 'container-fluid')
11+
12+
assert container_on_page.is_displayed()
13+
14+
driver.quit()
15+
16+
def test_evaluating_shadow_DOM():
17+
driver = webdriver.Chrome()
18+
driver.implicitly_wait(5)
19+
driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')
20+
21+
# div_el = driver.find_element(By.TAG_NAME, 'body')
22+
custom_element = driver.find_element(By.TAG_NAME, 'custom-checkbox-element')
23+
assert custom_element.is_displayed()
24+
print(custom_element.shadow_root)
25+
assert custom_element.shadow_root
26+
div_children = custom_element.shadow_root.find_element(By.CSS_SELECTOR, 'input[type=checkbox]')
27+
print(div_children)
28+
assert div_children.is_displayed()
29+
30+
driver.quit()
31+
32+
def test_optimized_locator():
33+
driver = webdriver.Chrome()
34+
driver.get('https://www.selenium.dev/')
35+
36+
nested_element = driver.find_element(By.CSS_SELECTOR, '.td-home #announcement-banner')
37+
38+
assert nested_element.is_displayed()
39+
40+
driver.quit()
41+
42+
def test_all_matching_elements():
43+
driver = webdriver.Chrome()
44+
driver.get('https://www.selenium.dev/')
45+
46+
header_two_elements = driver.find_elements(By.TAG_NAME, 'h2')
47+
48+
assert len(header_two_elements) > 1
49+
50+
for header_element in header_two_elements:
51+
print(header_element.text)
52+
53+
driver.quit()
54+
55+
def test_find_elements_from_element():
56+
driver = webdriver.Chrome()
57+
driver.get('https://www.selenium.dev/')
58+
59+
main_element = driver.find_element(By.TAG_NAME, 'main')
60+
svg_elements = main_element.find_elements(By.TAG_NAME, 'svg')
61+
62+
assert len(svg_elements) > 1
63+
64+
for svg_element in svg_elements:
65+
print(svg_element.is_displayed())
66+
67+
68+
## get elements from parent element using XPATH
69+
## NOTE: in order to utilize XPATH from current element, you must add "." to beginning of path
70+
71+
header_tag = driver.find_element(By.TAG_NAME, 'header')
72+
# Get first element of tag 'ul'
73+
ul_tag = header_tag.find_element(By.XPATH, '//ul')
74+
75+
# get children of tag 'ul' with tag 'li'
76+
elements = ul_tag.find_elements(By.XPATH, './/li')
77+
assert len(elements) > 0
78+
79+
for element in elements:
80+
print(element.text)
81+
82+
driver.quit()
83+
84+
def test_get_active_element():
85+
driver = webdriver.Chrome()
86+
driver.get('https://www.selenium.dev/')
87+
88+
dropdown = driver.find_element(By.CSS_SELECTOR, '.nav-item.dropdown')
89+
dropdown.click()
90+
91+
active_element = driver.switch_to.active_element
92+
93+
assert active_element.get_attribute('href').endswith('#')
94+
95+
driver.quit()
296

0 commit comments

Comments
 (0)