From d2f612ee25b277399021f9ddf779a8250657a86f Mon Sep 17 00:00:00 2001 From: Pablo Martinez Date: Thu, 12 Dec 2024 17:06:48 -0800 Subject: [PATCH 1/2] Add code that is to be used in examples Moving the code to the appropriate files --- .../python/tests/elements/test_finders.py | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/examples/python/tests/elements/test_finders.py b/examples/python/tests/elements/test_finders.py index 53b695b6fc83..0874d993187a 100644 --- a/examples/python/tests/elements/test_finders.py +++ b/examples/python/tests/elements/test_finders.py @@ -1,2 +1,96 @@ 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') + + # div_el = driver.find_element(By.TAG_NAME, 'body') + custom_element = driver.find_element(By.TAG_NAME, 'custom-checkbox-element') + 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() + + 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') + + 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 + + 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') + assert len(elements) > 0 + + for element in elements: + print(element.text) + + 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() From fee6557835e91f848460a9d37b56d4edb7118ed3 Mon Sep 17 00:00:00 2001 From: Pablo Martinez Date: Thu, 12 Dec 2024 18:44:25 -0800 Subject: [PATCH 2/2] Updates to moving code for python Finders docs --- .../python/tests/elements/test_finders.py | 15 ++-- .../webdriver/elements/finders.en.md | 80 ++++--------------- .../webdriver/elements/finders.ja.md | 80 ++++--------------- .../webdriver/elements/finders.pt-br.md | 75 ++++------------- .../webdriver/elements/finders.zh-cn.md | 80 ++++--------------- 5 files changed, 70 insertions(+), 260 deletions(-) diff --git a/examples/python/tests/elements/test_finders.py b/examples/python/tests/elements/test_finders.py index 0874d993187a..a1d5c186adc7 100644 --- a/examples/python/tests/elements/test_finders.py +++ b/examples/python/tests/elements/test_finders.py @@ -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() @@ -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 @@ -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() diff --git a/website_and_docs/content/documentation/webdriver/elements/finders.en.md b/website_and_docs/content/documentation/webdriver/elements/finders.en.md index 08f8d82da1c6..5089e56198b3 100644 --- a/website_and_docs/content/documentation/webdriver/elements/finders.en.md +++ b/website_and_docs/content/documentation/webdriver/elements/finders.en.md @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -190,8 +187,8 @@ references to all fruits and vegetable list items will be returned in a collecti {{< tab header="Java" >}} List 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 plants = driver.FindElements(By.TagName("li")); @@ -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; @@ -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; @@ -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; diff --git a/website_and_docs/content/documentation/webdriver/elements/finders.ja.md b/website_and_docs/content/documentation/webdriver/elements/finders.ja.md index a5c2f0ceaa35..cb02f98245ac 100644 --- a/website_and_docs/content/documentation/webdriver/elements/finders.ja.md +++ b/website_and_docs/content/documentation/webdriver/elements/finders.ja.md @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -179,8 +176,8 @@ val fruit = driver.findElement(By.cssSelector("#fruits .tomatoes")) {{< tab header="Java" >}} List 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 plants = driver.FindElements(By.TagName("li")); @@ -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; @@ -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; @@ -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; diff --git a/website_and_docs/content/documentation/webdriver/elements/finders.pt-br.md b/website_and_docs/content/documentation/webdriver/elements/finders.pt-br.md index b0c426ef7770..06b0fcaadeb4 100644 --- a/website_and_docs/content/documentation/webdriver/elements/finders.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/elements/finders.pt-br.md @@ -44,8 +44,8 @@ dois elementos que têm um nome de classe de "tomatoes" então este método reto {{< 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")); @@ -75,9 +75,8 @@ ancestral do elemento indesejado, então invoque o find element nesse objeto: 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")); @@ -117,10 +116,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")); @@ -154,8 +151,8 @@ Para esse exemplo, utilizaremos o 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")); @@ -182,8 +179,8 @@ referências a todos os itens da lista de frutas e vegetais serão devolvidas em {{< tab header="Java" >}} List 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 plants = driver.FindElements(By.TagName("li")); @@ -212,20 +209,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() - - # Navegar até a URL -driver.get("https://www.example.com") - - # Obtém todos os elementos disponiveis com o nome da tag '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; @@ -328,27 +313,8 @@ Para realizar isso, o WebElement pai é encadeado com o 'findElements' para aces } } {{< /tab >}} - {{< tab header="Python" >}} -##get elements from parent element using TAG_NAME - - # Obtém o elemento com o nome da tag 'div' -element = driver.find_element(By.TAG_NAME, 'div') - - # Obtém todos os elementos disponíveis com o nome da tag '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; @@ -449,17 +415,8 @@ Ele é usado para rastrear (ou) encontrar um elemento DOM que tem o foco no cont } } {{< /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") - - # Obter atributo do elemento atualmente ativo - 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; diff --git a/website_and_docs/content/documentation/webdriver/elements/finders.zh-cn.md b/website_and_docs/content/documentation/webdriver/elements/finders.zh-cn.md index 37094837cf0d..5834ff7cc100 100644 --- a/website_and_docs/content/documentation/webdriver/elements/finders.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/elements/finders.zh-cn.md @@ -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")); @@ -78,9 +78,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")); @@ -120,10 +119,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")); @@ -158,8 +155,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")); @@ -187,8 +184,8 @@ references to all fruits and vegetable list items will be returned in a collecti {{< tab header="Java" >}} List 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 plants = driver.FindElements(By.TagName("li")); @@ -217,20 +214,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; @@ -333,32 +318,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; @@ -459,17 +420,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;