Skip to content
92 changes: 92 additions & 0 deletions examples/python/tests/elements/test_finders.py
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")
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Comment thread
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()
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 >}}
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Comment thread
diemol marked this conversation as resolved.
{{< gh-codeblock path="/examples/python/tests/elements/test_finders.py#L23">}}
{{< /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#L28-L29">}}
{{< /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#L47-L50">}}
{{< /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#L39" >}}
{{< /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#L62-L64" >}}
{{< /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 = element.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#L75-L78" >}}
{{< /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#L89-L90" >}}
{{< /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#L23">}}
{{< /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#L28-L29">}}
{{< /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#L47-L50">}}
{{< /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#L39" >}}
{{< /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#L62-L64" >}}
{{< /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 = element.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#L75-L78" >}}
{{< /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#L89-L90" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
using OpenQA.Selenium;
Expand Down
Loading
Loading