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

Update elements finders python docs #2099

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/python/tests/elements/test_finders.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,93 @@
from selenium import webdriver
from selenium.webdriver.common.by import By


def test_basic_finders():
driver = webdriver.Chrome()
driver.get('https://www.selenium.dev/')

body_on_page = driver.find_element(By.CLASS_NAME, 'td-home')
container_on_page = body_on_page.find_element(By.CLASS_NAME, 'container-fluid')

assert container_on_page.is_displayed()

driver.quit()

def test_evaluating_shadow_DOM():
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')

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()
assert custom_element.shadow_root
assert shadow_dom_input.is_displayed()

driver.quit()

def test_optimized_locator():
driver = webdriver.Chrome()
driver.get('https://www.selenium.dev/')

nested_element = driver.find_element(By.CSS_SELECTOR, '.td-home #announcement-banner')

assert nested_element.is_displayed()

driver.quit()

def test_all_matching_elements():
driver = webdriver.Chrome()
driver.get('https://www.selenium.dev/')

header_two_elements = driver.find_elements(By.TAG_NAME, 'h2')

assert len(header_two_elements) > 1

for header_element in header_two_elements:
print(header_element.text)

driver.quit()

def test_find_elements_from_element():
driver = webdriver.Chrome()
driver.get('https://www.selenium.dev/')

main_element = driver.find_element(By.TAG_NAME, 'main')
svg_elements = main_element.find_elements(By.TAG_NAME, 'svg')

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

header_tag = driver.find_element(By.TAG_NAME, 'header')
# Get first element of tag 'ul'
ul_tag = header_tag.find_element(By.XPATH, '//ul')

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

for element in elements:
print(element.text)

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

driver.quit()

def test_get_active_element():
driver = webdriver.Chrome()
driver.get('https://www.selenium.dev/')

dropdown = driver.find_element(By.CSS_SELECTOR, '.nav-item.dropdown')
dropdown.click()

active_element = driver.switch_to.active_element

assert active_element.get_attribute('href').endswith('#')

driver.quit()

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
Loading