Skip to content

Commit

Permalink
Updates to moving code for python Finders docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinez1 committed Dec 13, 2024
1 parent d2f612e commit fee6557
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 260 deletions.
15 changes: 6 additions & 9 deletions examples/python/tests/elements/test_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ def test_evaluating_shadow_DOM():
driver.implicitly_wait(5)
driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')

# div_el = driver.find_element(By.TAG_NAME, 'body')
custom_element = driver.find_element(By.TAG_NAME, 'custom-checkbox-element')
shadow_dom_input = custom_element.shadow_root.find_element(By.CSS_SELECTOR, 'input[type=checkbox]')

assert custom_element.is_displayed()
print(custom_element.shadow_root)
assert custom_element.shadow_root
div_children = custom_element.shadow_root.find_element(By.CSS_SELECTOR, 'input[type=checkbox]')
print(div_children)
assert div_children.is_displayed()
assert shadow_dom_input.is_displayed()

driver.quit()

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

assert len(svg_elements) > 1

for svg_element in svg_elements:
print(svg_element.is_displayed())


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

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

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

for element in elements:
print(element.text)

assert len(elements) > 0
assert len(svg_elements) > 1

driver.quit()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ two elements that have a class name of "tomatoes" so this method will return the
{{< tab header="Java" >}}
WebElement vegetable = driver.findElement(By.className("tomatoes"));
{{< /tab >}}
{{< tab header="Python" >}}
vegetable = driver.find_element(By.CLASS_NAME, "tomatoes")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9">}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var vegetable = driver.FindElement(By.ClassName("tomatoes"));
Expand Down Expand Up @@ -79,9 +79,8 @@ ancestor of the undesired element, then call find element on that object:
WebElement fruits = driver.findElement(By.id("fruits"));
WebElement fruit = fruits.findElement(By.className("tomatoes"));
{{< /tab >}}
{{< tab header="Python" >}}
fruits = driver.find_element(By.ID, "fruits")
fruit = fruits.find_element(By.CLASS_NAME,"tomatoes")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9-L10">}}
{{< /tab >}}
{{< tab header="CSharp" >}}
IWebElement fruits = driver.FindElement(By.Id("fruits"));
Expand Down Expand Up @@ -121,10 +120,8 @@ WebElement shadowHost = driver.findElement(By.cssSelector("#shadow_host"));
SearchContext shadowRoot = shadowHost.getShadowRoot();
WebElement shadowContent = shadowRoot.findElement(By.cssSelector("#shadow_content"));
{{< /tab >}}
{{< tab header="Python" >}}
shadow_host = driver.find_element(By.CSS_SELECTOR, '#shadow_host')
shadow_root = shadow_host.shadow_root
shadow_content = shadow_root.find_element(By.CSS_SELECTOR, '#shadow_content')
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L21-L22">}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var shadowHost = _driver.FindElement(By.CssSelector("#shadow_host"));
Expand Down Expand Up @@ -160,8 +157,8 @@ For this example, we'll use a CSS Selector:
{{< tab header="Java" >}}
WebElement fruit = driver.findElement(By.cssSelector("#fruits .tomatoes"));
{{< /tab >}}
{{< tab header="Python" >}}
fruit = driver.find_element(By.CSS_SELECTOR,"#fruits .tomatoes")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L34" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var fruit = driver.FindElement(By.CssSelector("#fruits .tomatoes"));
Expand Down Expand Up @@ -190,8 +187,8 @@ references to all fruits and vegetable list items will be returned in a collecti
{{< tab header="Java" >}}
List<WebElement> plants = driver.findElements(By.tagName("li"));
{{< /tab >}}
{{< tab header="Python" >}}
plants = driver.find_elements(By.TAG_NAME, "li")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L44" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
IReadOnlyList<IWebElement> plants = driver.FindElements(By.TagName("li"));
Expand Down Expand Up @@ -221,20 +218,8 @@ for (WebElement element : elements) {
System.out.println("Paragraph text:" + element.getText());
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()

# Navigate to Url
driver.get("https://www.example.com")

# Get all the elements available with tag name 'p'
elements = driver.find_elements(By.TAG_NAME, 'p')

for e in elements:
print(e.text)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L54-L61" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down Expand Up @@ -338,32 +323,8 @@ To achieve this, the parent WebElement is chained with 'findElements' to access
}
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.example.com")
##get elements from parent element using TAG_NAME

# Get element with tag name 'div'
element = driver.find_element(By.TAG_NAME, 'div')

# Get all the elements available with tag name 'p'
elements = element.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)

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

# 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')
for e in elements:
print(e.text)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L66-L74" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down Expand Up @@ -465,17 +426,8 @@ It is used to track (or) find DOM element which has the focus in the current bro
}
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.google.com")
driver.find_element(By.CSS_SELECTOR, '[name="q"]').send_keys("webElement")

# Get attribute of current active element
attr = driver.switch_to.active_element.get_attribute("title")
print(attr)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L85-L88" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Seleniumは、要素を一意に識別するための多数の組み込み[ロ
{{< tab header="Java" >}}
WebElement vegetable = driver.findElement(By.className("tomatoes"));
{{< /tab >}}
{{< tab header="Python" >}}
vegetable = driver.find_element(By.CLASS_NAME, "tomatoes")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9">}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var vegetable = driver.FindElement(By.ClassName("tomatoes"));
Expand Down Expand Up @@ -73,9 +73,8 @@ DOM全体で一意のロケーターを見つけるのではなく、検索を
WebElement fruits = driver.findElement(By.id("fruits"));
WebElement fruit = fruits.findElement(By.className("tomatoes"));
{{< /tab >}}
{{< tab header="Python" >}}
fruits = driver.find_element(By.ID, "fruits")
fruit = fruits.find_element(By.CLASS_NAME,"tomatoes")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L9-L10">}}
{{< /tab >}}
{{< tab header="CSharp" >}}
IWebElement fruits = driver.FindElement(By.Id("fruits"));
Expand Down Expand Up @@ -114,10 +113,8 @@ WebElement shadowHost = driver.findElement(By.cssSelector("#shadow_host"));
SearchContext shadowRoot = shadowHost.getShadowRoot();
WebElement shadowContent = shadowRoot.findElement(By.cssSelector("#shadow_content"));
{{< /tab >}}
{{< tab header="Python" >}}
shadow_host = driver.find_element(By.CSS_SELECTOR, '#shadow_host')
shadow_root = shadow_host.shadow_root
shadow_content = shadow_root.find_element(By.CSS_SELECTOR, '#shadow_content')
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L21-L22">}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var shadowHost = _driver.FindElement(By.CssSelector("#shadow_host"));
Expand Down Expand Up @@ -150,8 +147,8 @@ shadow_content = shadow_root.find_element(css: '#shadow_content')
{{< tab header="Java" >}}
WebElement fruit = driver.findElement(By.cssSelector("#fruits .tomatoes"));
{{< /tab >}}
{{< tab header="Python" >}}
fruit = driver.find_element(By.CSS_SELECTOR,"#fruits .tomatoes")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L34" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var fruit = driver.FindElement(By.CssSelector("#fruits .tomatoes"));
Expand Down Expand Up @@ -179,8 +176,8 @@ val fruit = driver.findElement(By.cssSelector("#fruits .tomatoes"))
{{< tab header="Java" >}}
List<WebElement> plants = driver.findElements(By.tagName("li"));
{{< /tab >}}
{{< tab header="Python" >}}
plants = driver.find_elements(By.TAG_NAME, "li")
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L44" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
IReadOnlyList<IWebElement> plants = driver.FindElements(By.TagName("li"));
Expand Down Expand Up @@ -208,20 +205,8 @@ for (WebElement element : elements) {
System.out.println("Paragraph text:" + element.getText());
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()

# Navigate to Url
driver.get("https://www.example.com")

# Get all the elements available with tag name 'p'
elements = driver.find_elements(By.TAG_NAME, 'p')

for e in elements:
print(e.text)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L54-L61" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down Expand Up @@ -324,32 +309,8 @@ fun main() {
}
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.example.com")
##get elements from parent element using TAG_NAME

# Get element with tag name 'div'
element = driver.find_element(By.TAG_NAME, 'div')

# Get all the elements available with tag name 'p'
elements = element.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)

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

# 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')
for e in elements:
print(e.text)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L66-L74" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down Expand Up @@ -450,17 +411,8 @@ namespace FindElementsFromElement {
}
}
{{< /tab >}}
{{< tab header="Python" >}}
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.google.com")
driver.find_element(By.CSS_SELECTOR, '[name="q"]').send_keys("webElement")

# Get attribute of current active element
attr = driver.switch_to.active_element.get_attribute("title")
print(attr)
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L85-L88" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down
Loading

0 comments on commit fee6557

Please sign in to comment.