Skip to content

Commit fee6557

Browse files
committed
Updates to moving code for python Finders docs
1 parent d2f612e commit fee6557

File tree

5 files changed

+70
-260
lines changed

5 files changed

+70
-260
lines changed

examples/python/tests/elements/test_finders.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ def test_evaluating_shadow_DOM():
1818
driver.implicitly_wait(5)
1919
driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')
2020

21-
# div_el = driver.find_element(By.TAG_NAME, 'body')
2221
custom_element = driver.find_element(By.TAG_NAME, 'custom-checkbox-element')
22+
shadow_dom_input = custom_element.shadow_root.find_element(By.CSS_SELECTOR, 'input[type=checkbox]')
23+
2324
assert custom_element.is_displayed()
24-
print(custom_element.shadow_root)
2525
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()
26+
assert shadow_dom_input.is_displayed()
2927

3028
driver.quit()
3129

@@ -59,12 +57,9 @@ def test_find_elements_from_element():
5957
main_element = driver.find_element(By.TAG_NAME, 'main')
6058
svg_elements = main_element.find_elements(By.TAG_NAME, 'svg')
6159

62-
assert len(svg_elements) > 1
63-
6460
for svg_element in svg_elements:
6561
print(svg_element.is_displayed())
6662

67-
6863
## get elements from parent element using XPATH
6964
## NOTE: in order to utilize XPATH from current element, you must add "." to beginning of path
7065

@@ -74,10 +69,12 @@ def test_find_elements_from_element():
7469

7570
# get children of tag 'ul' with tag 'li'
7671
elements = ul_tag.find_elements(By.XPATH, './/li')
77-
assert len(elements) > 0
7872

7973
for element in elements:
8074
print(element.text)
75+
76+
assert len(elements) > 0
77+
assert len(svg_elements) > 1
8178

8279
driver.quit()
8380

website_and_docs/content/documentation/webdriver/elements/finders.en.md

+16-64
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ two elements that have a class name of "tomatoes" so this method will return the
4646
{{< tab header="Java" >}}
4747
WebElement vegetable = driver.findElement(By.className("tomatoes"));
4848
{{< /tab >}}
49-
{{< tab header="Python" >}}
50-
vegetable = driver.find_element(By.CLASS_NAME, "tomatoes")
49+
{{< tab header="Python" text=true >}}
50+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9">}}
5151
{{< /tab >}}
5252
{{< tab header="CSharp" >}}
5353
var vegetable = driver.FindElement(By.ClassName("tomatoes"));
@@ -79,9 +79,8 @@ ancestor of the undesired element, then call find element on that object:
7979
WebElement fruits = driver.findElement(By.id("fruits"));
8080
WebElement fruit = fruits.findElement(By.className("tomatoes"));
8181
{{< /tab >}}
82-
{{< tab header="Python" >}}
83-
fruits = driver.find_element(By.ID, "fruits")
84-
fruit = fruits.find_element(By.CLASS_NAME,"tomatoes")
82+
{{< tab header="Python" text=true >}}
83+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9-L10">}}
8584
{{< /tab >}}
8685
{{< tab header="CSharp" >}}
8786
IWebElement fruits = driver.FindElement(By.Id("fruits"));
@@ -121,10 +120,8 @@ WebElement shadowHost = driver.findElement(By.cssSelector("#shadow_host"));
121120
SearchContext shadowRoot = shadowHost.getShadowRoot();
122121
WebElement shadowContent = shadowRoot.findElement(By.cssSelector("#shadow_content"));
123122
{{< /tab >}}
124-
{{< tab header="Python" >}}
125-
shadow_host = driver.find_element(By.CSS_SELECTOR, '#shadow_host')
126-
shadow_root = shadow_host.shadow_root
127-
shadow_content = shadow_root.find_element(By.CSS_SELECTOR, '#shadow_content')
123+
{{< tab header="Python" text=true >}}
124+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L21-L22">}}
128125
{{< /tab >}}
129126
{{< tab header="CSharp" >}}
130127
var shadowHost = _driver.FindElement(By.CssSelector("#shadow_host"));
@@ -160,8 +157,8 @@ For this example, we'll use a CSS Selector:
160157
{{< tab header="Java" >}}
161158
WebElement fruit = driver.findElement(By.cssSelector("#fruits .tomatoes"));
162159
{{< /tab >}}
163-
{{< tab header="Python" >}}
164-
fruit = driver.find_element(By.CSS_SELECTOR,"#fruits .tomatoes")
160+
{{< tab header="Python" text=true >}}
161+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L34" >}}
165162
{{< /tab >}}
166163
{{< tab header="CSharp" >}}
167164
var fruit = driver.FindElement(By.CssSelector("#fruits .tomatoes"));
@@ -190,8 +187,8 @@ references to all fruits and vegetable list items will be returned in a collecti
190187
{{< tab header="Java" >}}
191188
List<WebElement> plants = driver.findElements(By.tagName("li"));
192189
{{< /tab >}}
193-
{{< tab header="Python" >}}
194-
plants = driver.find_elements(By.TAG_NAME, "li")
190+
{{< tab header="Python" text=true >}}
191+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L44" >}}
195192
{{< /tab >}}
196193
{{< tab header="CSharp" >}}
197194
IReadOnlyList<IWebElement> plants = driver.FindElements(By.TagName("li"));
@@ -221,20 +218,8 @@ for (WebElement element : elements) {
221218
System.out.println("Paragraph text:" + element.getText());
222219
}
223220
{{< /tab >}}
224-
{{< tab header="Python" >}}
225-
from selenium import webdriver
226-
from selenium.webdriver.common.by import By
227-
228-
driver = webdriver.Firefox()
229-
230-
# Navigate to Url
231-
driver.get("https://www.example.com")
232-
233-
# Get all the elements available with tag name 'p'
234-
elements = driver.find_elements(By.TAG_NAME, 'p')
235-
236-
for e in elements:
237-
print(e.text)
221+
{{< tab header="Python" text=true >}}
222+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L54-L61" >}}
238223
{{< /tab >}}
239224
{{< tab header="CSharp" >}}
240225
using OpenQA.Selenium;
@@ -338,32 +323,8 @@ To achieve this, the parent WebElement is chained with 'findElements' to access
338323
}
339324
}
340325
{{< /tab >}}
341-
{{< tab header="Python" >}}
342-
from selenium import webdriver
343-
from selenium.webdriver.common.by import By
344-
345-
driver = webdriver.Chrome()
346-
driver.get("https://www.example.com")
347-
##get elements from parent element using TAG_NAME
348-
349-
# Get element with tag name 'div'
350-
element = driver.find_element(By.TAG_NAME, 'div')
351-
352-
# Get all the elements available with tag name 'p'
353-
elements = element.find_elements(By.TAG_NAME, 'p')
354-
for e in elements:
355-
print(e.text)
356-
357-
##get elements from parent element using XPATH
358-
##NOTE: in order to utilize XPATH from current element, you must add "." to beginning of path
359-
360-
# Get first element of tag 'ul'
361-
element = driver.find_element(By.XPATH, '//ul')
362-
363-
# get children of tag 'ul' with tag 'li'
364-
elements = driver.find_elements(By.XPATH, './/li')
365-
for e in elements:
366-
print(e.text)
326+
{{< tab header="Python" text=true >}}
327+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L66-L74" >}}
367328
{{< /tab >}}
368329
{{< tab header="CSharp" >}}
369330
using OpenQA.Selenium;
@@ -465,17 +426,8 @@ It is used to track (or) find DOM element which has the focus in the current bro
465426
}
466427
}
467428
{{< /tab >}}
468-
{{< tab header="Python" >}}
469-
from selenium import webdriver
470-
from selenium.webdriver.common.by import By
471-
472-
driver = webdriver.Chrome()
473-
driver.get("https://www.google.com")
474-
driver.find_element(By.CSS_SELECTOR, '[name="q"]').send_keys("webElement")
475-
476-
# Get attribute of current active element
477-
attr = driver.switch_to.active_element.get_attribute("title")
478-
print(attr)
429+
{{< tab header="Python" text=true >}}
430+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L85-L88" >}}
479431
{{< /tab >}}
480432
{{< tab header="CSharp" >}}
481433
using OpenQA.Selenium;

website_and_docs/content/documentation/webdriver/elements/finders.ja.md

+16-64
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ Seleniumは、要素を一意に識別するための多数の組み込み[ロ
4343
{{< tab header="Java" >}}
4444
WebElement vegetable = driver.findElement(By.className("tomatoes"));
4545
{{< /tab >}}
46-
{{< tab header="Python" >}}
47-
vegetable = driver.find_element(By.CLASS_NAME, "tomatoes")
46+
{{< tab header="Python" text=true >}}
47+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9">}}
4848
{{< /tab >}}
4949
{{< tab header="CSharp" >}}
5050
var vegetable = driver.FindElement(By.ClassName("tomatoes"));
@@ -73,9 +73,8 @@ DOM全体で一意のロケーターを見つけるのではなく、検索を
7373
WebElement fruits = driver.findElement(By.id("fruits"));
7474
WebElement fruit = fruits.findElement(By.className("tomatoes"));
7575
{{< /tab >}}
76-
{{< tab header="Python" >}}
77-
fruits = driver.find_element(By.ID, "fruits")
78-
fruit = fruits.find_element(By.CLASS_NAME,"tomatoes")
76+
{{< tab header="Python" text=true >}}
77+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9-L10">}}
7978
{{< /tab >}}
8079
{{< tab header="CSharp" >}}
8180
IWebElement fruits = driver.FindElement(By.Id("fruits"));
@@ -114,10 +113,8 @@ WebElement shadowHost = driver.findElement(By.cssSelector("#shadow_host"));
114113
SearchContext shadowRoot = shadowHost.getShadowRoot();
115114
WebElement shadowContent = shadowRoot.findElement(By.cssSelector("#shadow_content"));
116115
{{< /tab >}}
117-
{{< tab header="Python" >}}
118-
shadow_host = driver.find_element(By.CSS_SELECTOR, '#shadow_host')
119-
shadow_root = shadow_host.shadow_root
120-
shadow_content = shadow_root.find_element(By.CSS_SELECTOR, '#shadow_content')
116+
{{< tab header="Python" text=true >}}
117+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L21-L22">}}
121118
{{< /tab >}}
122119
{{< tab header="CSharp" >}}
123120
var shadowHost = _driver.FindElement(By.CssSelector("#shadow_host"));
@@ -150,8 +147,8 @@ shadow_content = shadow_root.find_element(css: '#shadow_content')
150147
{{< tab header="Java" >}}
151148
WebElement fruit = driver.findElement(By.cssSelector("#fruits .tomatoes"));
152149
{{< /tab >}}
153-
{{< tab header="Python" >}}
154-
fruit = driver.find_element(By.CSS_SELECTOR,"#fruits .tomatoes")
150+
{{< tab header="Python" text=true >}}
151+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L34" >}}
155152
{{< /tab >}}
156153
{{< tab header="CSharp" >}}
157154
var fruit = driver.FindElement(By.CssSelector("#fruits .tomatoes"));
@@ -179,8 +176,8 @@ val fruit = driver.findElement(By.cssSelector("#fruits .tomatoes"))
179176
{{< tab header="Java" >}}
180177
List<WebElement> plants = driver.findElements(By.tagName("li"));
181178
{{< /tab >}}
182-
{{< tab header="Python" >}}
183-
plants = driver.find_elements(By.TAG_NAME, "li")
179+
{{< tab header="Python" text=true >}}
180+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L44" >}}
184181
{{< /tab >}}
185182
{{< tab header="CSharp" >}}
186183
IReadOnlyList<IWebElement> plants = driver.FindElements(By.TagName("li"));
@@ -208,20 +205,8 @@ for (WebElement element : elements) {
208205
System.out.println("Paragraph text:" + element.getText());
209206
}
210207
{{< /tab >}}
211-
{{< tab header="Python" >}}
212-
from selenium import webdriver
213-
from selenium.webdriver.common.by import By
214-
215-
driver = webdriver.Firefox()
216-
217-
# Navigate to Url
218-
driver.get("https://www.example.com")
219-
220-
# Get all the elements available with tag name 'p'
221-
elements = driver.find_elements(By.TAG_NAME, 'p')
222-
223-
for e in elements:
224-
print(e.text)
208+
{{< tab header="Python" text=true >}}
209+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L54-L61" >}}
225210
{{< /tab >}}
226211
{{< tab header="CSharp" >}}
227212
using OpenQA.Selenium;
@@ -324,32 +309,8 @@ fun main() {
324309
}
325310
}
326311
{{< /tab >}}
327-
{{< tab header="Python" >}}
328-
from selenium import webdriver
329-
from selenium.webdriver.common.by import By
330-
331-
driver = webdriver.Chrome()
332-
driver.get("https://www.example.com")
333-
##get elements from parent element using TAG_NAME
334-
335-
# Get element with tag name 'div'
336-
element = driver.find_element(By.TAG_NAME, 'div')
337-
338-
# Get all the elements available with tag name 'p'
339-
elements = element.find_elements(By.TAG_NAME, 'p')
340-
for e in elements:
341-
print(e.text)
342-
343-
##get elements from parent element using XPATH
344-
##NOTE: in order to utilize XPATH from current element, you must add "." to beginning of path
345-
346-
# Get first element of tag 'ul'
347-
element = driver.find_element(By.XPATH, '//ul')
348-
349-
# get children of tag 'ul' with tag 'li'
350-
elements = driver.find_elements(By.XPATH, './/li')
351-
for e in elements:
352-
print(e.text)
312+
{{< tab header="Python" text=true >}}
313+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L66-L74" >}}
353314
{{< /tab >}}
354315
{{< tab header="CSharp" >}}
355316
using OpenQA.Selenium;
@@ -450,17 +411,8 @@ namespace FindElementsFromElement {
450411
}
451412
}
452413
{{< /tab >}}
453-
{{< tab header="Python" >}}
454-
from selenium import webdriver
455-
from selenium.webdriver.common.by import By
456-
457-
driver = webdriver.Chrome()
458-
driver.get("https://www.google.com")
459-
driver.find_element(By.CSS_SELECTOR, '[name="q"]').send_keys("webElement")
460-
461-
# Get attribute of current active element
462-
attr = driver.switch_to.active_element.get_attribute("title")
463-
print(attr)
414+
{{< tab header="Python" text=true >}}
415+
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L85-L88" >}}
464416
{{< /tab >}}
465417
{{< tab header="CSharp" >}}
466418
using OpenQA.Selenium;

0 commit comments

Comments
 (0)