From 1e9907ce303779e9700207a640200f65a7aa53b7 Mon Sep 17 00:00:00 2001 From: AutomatedTester Date: Thu, 18 Feb 2021 15:10:05 +0000 Subject: [PATCH 1/2] Adds documentation and examples for relative locator usage. --- .../content/webdriver/web_element.de.md | 258 +++++++++++++++--- .../content/webdriver/web_element.en.md | 211 ++++++++++++-- .../content/webdriver/web_element.es.md | 240 +++++++++++++--- .../content/webdriver/web_element.fr.md | 220 +++++++++++++-- .../content/webdriver/web_element.ja.md | 220 +++++++++++++-- .../content/webdriver/web_element.ko.md | 222 +++++++++++++-- .../content/webdriver/web_element.nl.md | 216 +++++++++++++-- .../content/webdriver/web_element.pt-br.md | 200 ++++++++++++-- .../content/webdriver/web_element.zh-cn.md | 214 +++++++++++++-- 9 files changed, 1751 insertions(+), 250 deletions(-) diff --git a/docs_source_files/content/webdriver/web_element.de.md b/docs_source_files/content/webdriver/web_element.de.md index bf1d7761f632..37764596ca54 100644 --- a/docs_source_files/content/webdriver/web_element.de.md +++ b/docs_source_files/content/webdriver/web_element.de.md @@ -7,12 +7,12 @@ WebElement repräsentiert ein Element im DOM. WebElemente können ausgehend vom Wurzelknoten (root) gesucht werden oder ausgehend von einem anderen WebElement. Die WebDriver API stellt Methoden zur Verfügung, die es ermöglichen WebElemente, -aufgrund ihrer Eigenschaften zu finden wie z.B. ID, Name, Class (CSS-Klasse), Xpath, +aufgrund ihrer Eigenschaften zu finden wie z.B. ID, Name, Class (CSS-Klasse), Xpath, CSS Selektoren, Text einer Verlinkung, etc. ## Elemente finden -findElement wird verwendet, um ein WebElement zu finden. Die Funktion liefert +findElement wird verwendet, um ein WebElement zu finden. Die Funktion liefert als Rückgabewert die Referenz auf ein einzelnes WebElement, dieses kann dann für weitere Aktionen verwendet werden. @@ -222,6 +222,174 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +{{% notice info %}} + Page being translated from +English. Do you speak German? Help us to translate +it by sending us pull requests! +{{% /notice %}} + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## Finden eines Elements innerhalb anderer Elemente Es ist auch möglich Elemente zu suchen, basierend auf einem existierenden @@ -305,7 +473,7 @@ searchBox.sendKeys("webdriver") ## Finden von mehreren Elementen innerhalb eines anderen Elements findElements wird verwenden um entsprechende WebElemente im Kontext eines -Eltern-Elementen zu finden. Um dies durchzuführen wird die 'findElements'-Methode +Eltern-Elementen zu finden. Um dies durchzuführen wird die 'findElements'-Methode vom entsprechenden WebElement ausgeführt. {{< code-tab >}} @@ -384,13 +552,13 @@ namespace FindElementsFromElement { begin # Navigate to URL driver.get 'https://www.example.com' - + # Get element with tag name 'div' element = driver.find_element(:tag_name,'div') - + # Get all the elements available with tag name 'p' elements = element.find_elements(:tag_name,'p') - + elements.each { |e| puts e.text } @@ -400,17 +568,17 @@ namespace FindElementsFromElement { {{< / code-panel >}} {{< code-panel language="javascript" >}} const {Builder, By} = require('selenium-webdriver'); - - (async function example() { + + (async function example() { let driver = new Builder() .forBrowser('chrome') .build(); - + await driver.get('https://www.example.com'); - + // Get element with tag name 'div' let element = driver.findElement(By.css("div")); - + // Get all the elements available with tag name 'p' let elements = await element.findElements(By.css("p")); for(let e of elements) { @@ -421,15 +589,15 @@ namespace FindElementsFromElement { {{< code-panel language="kotlin" >}} import org.openqa.selenium.By import org.openqa.selenium.chrome.ChromeDriver - + fun main() { val driver = ChromeDriver() try { driver.get("https://example.com") - + // Get element with tag name 'div' val element = driver.findElement(By.tagName("div")) - + // Get all the elements available with tag name 'p' val elements = element.findElements(By.tagName("p")) for (e in elements) { @@ -441,7 +609,7 @@ namespace FindElementsFromElement { } {{< / code-panel >}} {{< / code-tab >}} - + ## Aktives Element Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolgen. @@ -450,14 +618,14 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg {{< code-panel language="java" >}} import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; - + public class activeElementTest { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); try { driver.get("http://www.google.com"); driver.findElement(By.cssSelector("[name='q']")).sendKeys("webElement"); - + // Get attribute of current active element String attr = driver.switchTo().activeElement().getAttribute("title"); System.out.println(attr); @@ -470,11 +638,11 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg {{< code-panel language="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) @@ -482,7 +650,7 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg {{< code-panel language="csharp" >}} using OpenQA.Selenium; using OpenQA.Selenium.Chrome; - + namespace ActiveElement { class ActiveElement { public static void Main(string[] args) { @@ -491,7 +659,7 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg // Navigate to Url driver.Navigate().GoToUrl("https://www.google.com"); driver.FindElement(By.CssSelector("[name='q']")).SendKeys("webElement"); - + // Get attribute of current active element string attr = driver.SwitchTo().ActiveElement().GetAttribute("title"); System.Console.WriteLine(attr); @@ -508,7 +676,7 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg begin driver.get 'https://www.google.com' driver.find_element(css: '[name="q"]').send_keys('webElement') - + # Get attribute of current active element attr = driver.switch_to.active_element.attribute('title') puts attr @@ -518,12 +686,12 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg {{< / code-panel >}} {{< code-panel language="javascript" >}} const {Builder, By} = require('selenium-webdriver'); - + (async function example() { let driver = await new Builder().forBrowser('chrome').build(); await driver.get('https://www.google.com'); await driver.findElement(By.css('[name="q"]')).sendKeys("webElement"); - + // Get attribute of current active element let attr = await driver.switchTo().activeElement().getAttribute("title"); console.log(`${attr}`) @@ -532,13 +700,13 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg {{< code-panel language="kotlin" >}} import org.openqa.selenium.By import org.openqa.selenium.chrome.ChromeDriver - + fun main() { val driver = ChromeDriver() try { driver.get("https://www.google.com") driver.findElement(By.cssSelector("[name='q']")).sendKeys("webElement") - + // Get attribute of current active element val attr = driver.switchTo().activeElement().getAttribute("title") print(attr) @@ -552,22 +720,22 @@ Wird verwendet um das Element, das den Fokus hat zu finden oder um es zu verfolg ## Element aktiviert? Die Methode dient dazu, um festzustellen, ob das referenzierte -Element prüfen ob es aktiviert oder deaktiviert ist. Der Rückgabewert +Element prüfen ob es aktiviert oder deaktiviert ist. Der Rückgabewert ist ein Boolean, **Wahr** wenn das Element **aktiv** (enabled) ist, andernfalls **Falsch**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + // Returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -596,9 +764,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -611,14 +779,14 @@ ist oder nicht. Die Methode wird häufig verwendet mit WebElementen vom Typ Checkbox, Radiobutton, Eingabefelder und anderen Elementen die auswählbare Optionen anbieten. -Der Rückgabewert ist ein Boolean, **Wahr** wenn das Element im aktuellen +Der Rückgabewert ist ein Boolean, **Wahr** wenn das Element im aktuellen Browserkontext **selektiert** ist, andernfalls den Wert **Falsch**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -632,7 +800,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -651,9 +819,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -666,7 +834,7 @@ des referenzierten Elements, zu ermitteln. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -682,7 +850,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -701,9 +869,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -734,7 +902,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -751,7 +919,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -819,7 +987,7 @@ cssValue = driver.find_element(:link_text, 'More information...').css_value('col {{< code-panel language="javascript" >}} // Navigate to Url await driver.get('https://www.example.com'); - + // Retrieves the computed style property 'color' of linktext let cssValue = await driver.findElement(By.linkText("More information...")).getCssValue('color'); {{< / code-panel >}} diff --git a/docs_source_files/content/webdriver/web_element.en.md b/docs_source_files/content/webdriver/web_element.en.md index 35f43690b973..56b3dd242746 100644 --- a/docs_source_files/content/webdriver/web_element.en.md +++ b/docs_source_files/content/webdriver/web_element.en.md @@ -220,6 +220,169 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + + ## Find Element From Element It is used to find a child element within the context of parent element. @@ -546,23 +709,23 @@ It is used to track (or) find DOM element which has the focus in the current bro ## Is Element Enabled -This method is used to check if the connected Element +This method is used to check if the connected Element is enabled or disabled on a webpage. -Returns a boolean value, **True** if the connected element is +Returns a boolean value, **True** if the connected element is **enabled** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -591,9 +754,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -601,18 +764,18 @@ let element = await driver.findElement(By.name("btnK")).isEnabled(); ## Is Element Selected -This method determines if the referenced Element -is _Selected_ or not. This method is widely used on +This method determines if the referenced Element +is _Selected_ or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements. -Returns a boolean value, **True** if referenced element is +Returns a boolean value, **True** if referenced element is **selected** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -626,7 +789,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -645,9 +808,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -655,12 +818,12 @@ let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type") ## Get Element TagName -It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) +It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) of the referenced Element which has the focus in the current browsing context. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -676,7 +839,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -695,9 +858,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -705,7 +868,7 @@ let value = await driver.findElement(By.css('h1')).getTagName(); ## Get Element Rect -It is used to fetch the dimensions and coordinates +It is used to fetch the dimensions and coordinates of the referenced element. The fetched data body contain the following details: @@ -728,7 +891,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -745,7 +908,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -770,7 +933,7 @@ println(res.getX()) ## Get Element CSS Value -Retrieves the value of specified computed style property +Retrieves the value of specified computed style property of an element in the current browsing context. {{< code-tab >}} diff --git a/docs_source_files/content/webdriver/web_element.es.md b/docs_source_files/content/webdriver/web_element.es.md index 33805f487c92..b6126dcead61 100644 --- a/docs_source_files/content/webdriver/web_element.es.md +++ b/docs_source_files/content/webdriver/web_element.es.md @@ -4,17 +4,17 @@ weight: 9 --- WebElement representa un elemento del DOM. Los WebElements se pueden -encontrar buscando desde la raíz del documento utilizando una instancia de -WebDriver o buscando en otra WebElement. +encontrar buscando desde la raíz del documento utilizando una instancia de +WebDriver o buscando en otra WebElement. -El API WebDriver proporciona métodos integrados para encontrar los -elementos web que son basados en diferentes propiedades como ID, +El API WebDriver proporciona métodos integrados para encontrar los +elementos web que son basados en diferentes propiedades como ID, Nombre, Clase, XPath, Selectores CSS, Texto de enlace, etc. ## Find Element Se utiliza para encontrar un elemento y devuelve la primera -referencia única de WebElement que coincide, que puede usarse para +referencia única de WebElement que coincide, que puede usarse para acciones futuras con el elemento {{< code-tab >}} @@ -98,10 +98,10 @@ searchBox.sendKeys("webdriver") ## Find Elements -Similar a 'Find Element', pero devuelve una lista -de elementos web coincidentes. -Para usar un WebElement particular de la lista, -debes recorrerla lista de elementos para realizar +Similar a 'Find Element', pero devuelve una lista +de elementos web coincidentes. +Para usar un WebElement particular de la lista, +debes recorrerla lista de elementos para realizar acciones con el elemento seleccionado. {{< code-tab >}} @@ -225,6 +225,174 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +{{% notice info %}} + Page being translated from +English. Do you speak Spanish? Help us to translate +it by sending us pull requests! +{{% /notice %}} + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## Find Element desde Element Se utiliza para encontrar un elemento hijo dentro @@ -307,9 +475,9 @@ searchBox.sendKeys("webdriver") ## Find Elements desde Element -Se utiliza para encontrar una lista de elementos +Se utiliza para encontrar una lista de elementos hijos dentro del contexto del elemento padre. -Para lograr esto, el WebElement primario se encadena +Para lograr esto, el WebElement primario se encadena con 'findElements'para acceder a elementos secundarios. {{< code-tab >}} @@ -448,7 +616,7 @@ namespace FindElementsFromElement { ## Get Active Element -Se utiliza para rastrear (o) encontrar el elemento DOM +Se utiliza para rastrear (o) encontrar el elemento DOM que tiene el foco en el contexto de navegación actual. {{< code-tab >}} @@ -559,21 +727,21 @@ que tiene el foco en el contexto de navegación actual. Este método se utiliza para comprobar si el elemento conectado está habilitado o deshabilitado en una página web. Devuelve un valor booleano, **True** si el elemento conectado es -**habilitado** en el contexto de navegación actual, +**habilitado** en el contexto de navegación actual, de lo contrario, devuelve **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -602,9 +770,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -612,18 +780,18 @@ let element = await driver.findElement(By.name("btnK")).isEnabled(); ## Is Element Selected -This method determines if the referenced Element -is _Selected_ or not. This method is widely used on +This method determines if the referenced Element +is _Selected_ or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements. -Returns a boolean value, **True** if referenced element is +Returns a boolean value, **True** if referenced element is **selected** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -637,7 +805,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -656,9 +824,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -666,12 +834,12 @@ let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type") ## Get Element TagName -It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) +It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) of the referenced Element which has the focus in the current browsing context. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -687,7 +855,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -706,9 +874,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -716,7 +884,7 @@ let value = await driver.findElement(By.css('h1')).getTagName(); ## Get Element Rect -It is used to fetch the dimensions and coordinates +It is used to fetch the dimensions and coordinates of the referenced element. The fetched data body contain the following details: @@ -739,7 +907,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -756,7 +924,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -781,7 +949,7 @@ println(res.getX()) ## Get Element CSS Value -Retrieves the value of specified computed style property +Retrieves the value of specified computed style property of an element in the current browsing context. {{< code-tab >}} @@ -824,7 +992,7 @@ cssValue = driver.find_element(:link_text, 'More information...').css_value('col {{< code-panel language="javascript" >}} // Navigate to Url await driver.get('https://www.example.com'); - + // Retrieves the computed style property 'color' of linktext let cssValue = await driver.findElement(By.linkText("More information...")).getCssValue('color'); {{< / code-panel >}} diff --git a/docs_source_files/content/webdriver/web_element.fr.md b/docs_source_files/content/webdriver/web_element.fr.md index 586b12c29367..c7d98a90495d 100644 --- a/docs_source_files/content/webdriver/web_element.fr.md +++ b/docs_source_files/content/webdriver/web_element.fr.md @@ -220,6 +220,174 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +{{% notice info %}} + Page being translated from +English. Do you speak French? Help us to translate +it by sending us pull requests! +{{% /notice %}} + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## Find Element From Element Il est utilisé pour rechercher un élément enfant dans le contexte de l'élément parent. @@ -546,23 +714,23 @@ Il est utilisé pour suivre (ou) trouver l'élément DOM qui a le focus dans le ## Is Element Enabled -This method is used to check if the connected Element +This method is used to check if the connected Element is enabled or disabled on a webpage. -Returns a boolean value, **True** if the connected element is +Returns a boolean value, **True** if the connected element is **enabled** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -591,9 +759,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -601,18 +769,18 @@ let element = await driver.findElement(By.name("btnK")).isEnabled(); ## Is Element Selected -This method determines if the referenced Element -is _Selected_ or not. This method is widely used on +This method determines if the referenced Element +is _Selected_ or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements. -Returns a boolean value, **True** if referenced element is +Returns a boolean value, **True** if referenced element is **selected** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -626,7 +794,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -645,9 +813,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -655,12 +823,12 @@ let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type") ## Get Element TagName -It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) +It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) of the referenced Element which has the focus in the current browsing context. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -676,7 +844,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -695,9 +863,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -705,8 +873,8 @@ let value = await driver.findElement(By.css('h1')).getTagName(); ## Get Element Rect -It is used to fetch the dimensions and coordinates -of the referenced element. +It is used to fetch the dimensions and coordinates +of the referenced element. The fetched data body contain the following details: * X-axis position from the top-left corner of the element @@ -728,7 +896,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -745,7 +913,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -770,7 +938,7 @@ println(res.getX()) ## Get Element CSS Value -Retrieves the value of specified computed style property +Retrieves the value of specified computed style property of an element in the current browsing context. {{< code-tab >}} @@ -813,7 +981,7 @@ cssValue = driver.find_element(:link_text, 'More information...').css_value('col {{< code-panel language="javascript" >}} // Navigate to Url await driver.get('https://www.example.com'); - + // Retrieves the computed style property 'color' of linktext let cssValue = await driver.findElement(By.linkText("More information...")).getCssValue('color'); {{< / code-panel >}} diff --git a/docs_source_files/content/webdriver/web_element.ja.md b/docs_source_files/content/webdriver/web_element.ja.md index c7eddc22b746..5b8a3ea4bf95 100644 --- a/docs_source_files/content/webdriver/web_element.ja.md +++ b/docs_source_files/content/webdriver/web_element.ja.md @@ -218,6 +218,174 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +{{% notice info %}} + Page being translated from +English. Do you speak Japanese? Help us to translate +it by sending us pull requests! +{{% /notice %}} + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## 要素から要素の検索 親要素のコンテキスト内で子要素を見つけるために使用します。 @@ -544,23 +712,23 @@ namespace FindElementsFromElement { ## Is Element Enabled -This method is used to check if the connected Element +This method is used to check if the connected Element is enabled or disabled on a webpage. -Returns a boolean value, **True** if the connected element is +Returns a boolean value, **True** if the connected element is **enabled** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -589,9 +757,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -599,18 +767,18 @@ let element = await driver.findElement(By.name("btnK")).isEnabled(); ## Is Element Selected -This method determines if the referenced Element -is _Selected_ or not. This method is widely used on +This method determines if the referenced Element +is _Selected_ or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements. -Returns a boolean value, **True** if referenced element is +Returns a boolean value, **True** if referenced element is **selected** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -624,7 +792,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -643,9 +811,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -653,12 +821,12 @@ let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type") ## Get Element TagName -It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) +It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) of the referenced Element which has the focus in the current browsing context. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -674,7 +842,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -693,9 +861,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -703,8 +871,8 @@ let value = await driver.findElement(By.css('h1')).getTagName(); ## Get Element Rect -It is used to fetch the dimensions and coordinates -of the referenced element. +It is used to fetch the dimensions and coordinates +of the referenced element. The fetched data body contain the following details: * X-axis position from the top-left corner of the element @@ -726,7 +894,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -743,7 +911,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -768,7 +936,7 @@ println(res.getX()) ## Get Element CSS Value -Retrieves the value of specified computed style property +Retrieves the value of specified computed style property of an element in the current browsing context. {{< code-tab >}} @@ -811,7 +979,7 @@ cssValue = driver.find_element(:link_text, 'More information...').css_value('col {{< code-panel language="javascript" >}} // Navigate to Url await driver.get('https://www.example.com'); - + // Retrieves the computed style property 'color' of linktext let cssValue = await driver.findElement(By.linkText("More information...")).getCssValue('color'); {{< / code-panel >}} diff --git a/docs_source_files/content/webdriver/web_element.ko.md b/docs_source_files/content/webdriver/web_element.ko.md index dff6282a0a3b..dda43856eed8 100644 --- a/docs_source_files/content/webdriver/web_element.ko.md +++ b/docs_source_files/content/webdriver/web_element.ko.md @@ -4,7 +4,7 @@ weight: 9 --- {{% notice info %}} - Page being translated from + Page being translated from English to Korean. Do you speak Korean? Help us to translate it by sending us pull requests! {{% /notice %}} @@ -226,6 +226,174 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +{{% notice info %}} + Page being translated from +English. Do you speak Korean? Help us to translate +it by sending us pull requests! +{{% /notice %}} + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## Find Element From Element It is used to find a child element within the context of parent element. @@ -552,23 +720,23 @@ It is used to track (or) find DOM element which has the focus in the current bro ## Is Element Enabled -This method is used to check if the connected Element +This method is used to check if the connected Element is enabled or disabled on a webpage. -Returns a boolean value, **True** if the connected element is +Returns a boolean value, **True** if the connected element is **enabled** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -597,9 +765,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -607,18 +775,18 @@ let element = await driver.findElement(By.name("btnK")).isEnabled(); ## Is Element Selected -This method determines if the referenced Element -is _Selected_ or not. This method is widely used on +This method determines if the referenced Element +is _Selected_ or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements. -Returns a boolean value, **True** if referenced element is +Returns a boolean value, **True** if referenced element is **selected** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -632,7 +800,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -651,9 +819,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -661,12 +829,12 @@ let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type") ## Get Element TagName -It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) +It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) of the referenced Element which has the focus in the current browsing context. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -682,7 +850,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -701,9 +869,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -711,8 +879,8 @@ let value = await driver.findElement(By.css('h1')).getTagName(); ## Get Element Rect -It is used to fetch the dimensions and coordinates -of the referenced element. +It is used to fetch the dimensions and coordinates +of the referenced element. The fetched data body contain the following details: * X-axis position from the top-left corner of the element @@ -734,7 +902,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -751,7 +919,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -776,7 +944,7 @@ println(res.getX()) ## Get Element CSS Value -Retrieves the value of specified computed style property +Retrieves the value of specified computed style property of an element in the current browsing context. {{< code-tab >}} @@ -819,7 +987,7 @@ cssValue = driver.find_element(:link_text, 'More information...').css_value('col {{< code-panel language="javascript" >}} // Navigate to Url await driver.get('https://www.example.com'); - + // Retrieves the computed style property 'color' of linktext let cssValue = await driver.findElement(By.linkText("More information...")).getCssValue('color'); {{< / code-panel >}} diff --git a/docs_source_files/content/webdriver/web_element.nl.md b/docs_source_files/content/webdriver/web_element.nl.md index 497045ac78e3..10ab1d830957 100644 --- a/docs_source_files/content/webdriver/web_element.nl.md +++ b/docs_source_files/content/webdriver/web_element.nl.md @@ -4,7 +4,7 @@ weight: 9 --- {{% notice info %}} - Page being translated from + Page being translated from English to Dutch. Do you speak Dutch? Help us to translate it by sending us pull requests! {{% /notice %}} @@ -226,6 +226,168 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## Find Element From Element It is used to find a child element within the context of parent element. @@ -552,23 +714,23 @@ It is used to track (or) find DOM element which has the focus in the current bro ## Is Element Enabled -This method is used to check if the connected Element +This method is used to check if the connected Element is enabled or disabled on a webpage. -Returns a boolean value, **True** if the connected element is +Returns a boolean value, **True** if the connected element is **enabled** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -597,9 +759,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -607,18 +769,18 @@ let element = await driver.findElement(By.name("btnK")).isEnabled(); ## Is Element Selected -This method determines if the referenced Element -is _Selected_ or not. This method is widely used on +This method determines if the referenced Element +is _Selected_ or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements. -Returns a boolean value, **True** if referenced element is +Returns a boolean value, **True** if referenced element is **selected** in the current browsing context else returns **false**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -632,7 +794,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -651,9 +813,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -661,12 +823,12 @@ let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type") ## Get Element TagName -It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) +It is used to fetch the [TagName](https://www.w3.org/TR/webdriver/#dfn-get-element-tag-name) of the referenced Element which has the focus in the current browsing context. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -682,7 +844,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -701,9 +863,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -711,8 +873,8 @@ let value = await driver.findElement(By.css('h1')).getTagName(); ## Get Element Rect -It is used to fetch the dimensions and coordinates -of the referenced element. +It is used to fetch the dimensions and coordinates +of the referenced element. The fetched data body contain the following details: * X-axis position from the top-left corner of the element @@ -734,7 +896,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -751,7 +913,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -776,7 +938,7 @@ println(res.getX()) ## Get Element CSS Value -Retrieves the value of specified computed style property +Retrieves the value of specified computed style property of an element in the current browsing context. {{< code-tab >}} @@ -819,7 +981,7 @@ cssValue = driver.find_element(:link_text, 'More information...').css_value('col {{< code-panel language="javascript" >}} // Navigate to Url await driver.get('https://www.example.com'); - + // Retrieves the computed style property 'color' of linktext let cssValue = await driver.findElement(By.linkText("More information...")).getCssValue('color'); {{< / code-panel >}} diff --git a/docs_source_files/content/webdriver/web_element.pt-br.md b/docs_source_files/content/webdriver/web_element.pt-br.md index cd60d55bc1ba..9fbbaa033bbc 100644 --- a/docs_source_files/content/webdriver/web_element.pt-br.md +++ b/docs_source_files/content/webdriver/web_element.pt-br.md @@ -224,6 +224,174 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +{{% notice info %}} + Page being translated from +English. Do you speak Portuguese? Help us to translate +it by sending us pull requests! +{{% /notice %}} + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## Encontrar Elemento a partir do Elemento É usado para localizar um elemento filho dentro do contexto do elemento @@ -561,16 +729,16 @@ retorna **False**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -599,9 +767,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -620,9 +788,9 @@ Retorna um valor booleano, **true** se o elemento referenciado for {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -636,7 +804,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -655,9 +823,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -670,7 +838,7 @@ do elemento referenciado que tem o foco no contexto de navegação atual. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -686,7 +854,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -705,9 +873,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -738,7 +906,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -755,7 +923,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} diff --git a/docs_source_files/content/webdriver/web_element.zh-cn.md b/docs_source_files/content/webdriver/web_element.zh-cn.md index 1ac165f4c59c..05a6bf3b81a8 100644 --- a/docs_source_files/content/webdriver/web_element.zh-cn.md +++ b/docs_source_files/content/webdriver/web_element.zh-cn.md @@ -5,7 +5,7 @@ weight: 9 WebElement表示DOM元素. -可以通过使用WebDriver实例从文档根节点进行搜索, +可以通过使用WebDriver实例从文档根节点进行搜索, 或者在另一个WebElement下进行搜索来找到WebElement. WebDriver API提供了内置方法来查找基于不同属性的WebElement @@ -98,7 +98,7 @@ searchBox.sendKeys("webdriver") ## Find Elements 与"Find Element"相似, 但返回的是匹配WebElement列表. -要使用列表中的特定WebElement, +要使用列表中的特定WebElement, 您需要遍历元素列表以对选定元素执行操作. {{< code-tab >}} @@ -222,6 +222,174 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} +### Find Elements using relative locators + +{{% notice info %}} + Page being translated from +English. Do you speak Chinese (simplified)? Help us to translate +it by sending us pull requests! +{{% /notice %}} + +Note: This feature is available in Selenium 4 or later + +These feature allows you to find Elements in the DOM based off the position +of another element using relative locations like `above`, `below`, `toLeftOf`, +and `toRightOf`. The Elements should be returned in proximity order. + + +{{< code-tab >}} + {{< code-panel language="java" >}} + import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; + import org.openqa.selenium.WebElement; + import org.openqa.selenium.chrome.ChromeDriver; + import java.util.List; + + import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + + public class findElementsUsingRelativeLocators { + public static void main(String[] args) { + WebDriver driver = new ChromeDriver(); + try { + driver.get("https://example.com"); + + // Get element with tag name 'div' + WebElement fromElement = driver.findElement(By.tagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement` + List elements = driver.findElements( + withTagName("p").above(fromElement) + ); + for (WebElement e : elements) { + System.out.println(e.getText()); + } + } finally { + driver.quit(); + } + } + } + {{< / code-panel >}} + {{< code-panel language="python" >}} +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.relative_locator import with_tag_name + +driver = webdriver.Chrome() +driver.get("https://www.example.com") + +# Get element with tag name 'div' +form_element = driver.find_element(By.TAG_NAME, 'div') + +# Get all the elements available with tag name 'p' above +# WebElement `fromElement`' +elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) +for e in elements: + print(e.text) + {{< / code-panel >}} + {{< code-panel language="csharp" >}} +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using System.Collections.Generic; + +namespace FindElementsUsingRelativeLocators { + class FindElementsUsingRelativeLocators { + public static void Main(string[] args) { + IWebDriver driver = new ChromeDriver(); + try { + driver.Navigate().GoToUrl("https://example.com"); + + // Get element with tag name 'div' + IWebElement fromElement = driver.FindElement(By.TagName("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + ReadOnlyCollection elements = driver.FindElements( + RelativeBy.WithTagName("p").Above(fromElement) + ); + foreach(IWebElement e in elements) { + System.Console.WriteLine(e.Text); + } + } finally { + driver.Quit(); + } + } + } +} + {{< / code-panel >}} + {{< code-panel language="ruby" >}} + require 'selenium-webdriver' + driver = Selenium::WebDriver.for :chrome + begin + # Navigate to URL + driver.get 'https://www.example.com' + + # Get element with tag name 'div' + from_element = driver.find_element(:tag_name,'div') + + # Get all the elements available with tag name 'p' above + # WebElement `fromElement`' + elements = element.find_elements( + relative: {tag_name: 'p', above: from_element} + ) + + elements.each { |e| + puts e.text + } + ensure + driver.quit + end + {{< / code-panel >}} + {{< code-panel language="javascript" >}} + const {Builder, By} = require('selenium-webdriver'); + + (async function example() { + let driver = new Builder() + .forBrowser('chrome') + .build(); + + await driver.get('https://www.example.com'); + + // Get element with tag name 'div' + let fromElement = await driver.findElement(By.css("div")); + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement' + let elements = await driver.findElements( + withTagName('p').above(fromElement) + ); + for(let e of elements) { + console.log(await e.getText()); + } + })(); + {{< / code-panel >}} + {{< code-panel language="kotlin" >}} + import org.openqa.selenium.By + import org.openqa.selenium.chrome.ChromeDriver + + fun main() { + val driver = ChromeDriver() + try { + driver.get("https://example.com") + + // Get element with tag name 'div' + val fromElement = driver.findElement(By.tagName("div")) + + // Get all the elements available with tag name 'p' above + // WebElement `fromElement'' + val elements = driver.findElements( + withTagName("p").above(fromElement) + ) + for (e in elements) { + println(e.text) + } + } finally { + driver.quit() + } + } + {{< / code-panel >}} +{{< / code-tab >}} + ## Find Element From Element 此方法用于在父元素的上下文中查找子元素. @@ -550,22 +718,22 @@ namespace FindElementsFromElement { ## Is Element Enabled 此方法用于检查网页上连接的元素是否被启用或禁用. -返回一个布尔值, +返回一个布尔值, 如果在当前浏览上下文中`启用`了连接的元素, 则返回True; 否则返回`false` . {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/"); - + //returns true if element is enabled else returns false boolean value = driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="python" >}} # Navigate to url driver.get("http://www.google.com") - + # Returns true if element is enabled else returns false value = driver.find_element(By.NAME, 'btnK').is_enabled() {{< / code-panel >}} @@ -594,9 +762,9 @@ await driver.get('https://www.google.com'); let element = await driver.findElement(By.name("btnK")).isEnabled(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.google.com/") - + //returns true if element is enabled else returns false val attr = driver.findElement(By.name("btnK")).isEnabled() {{< / code-panel >}} @@ -607,15 +775,15 @@ let element = await driver.findElement(By.name("btnK")).isEnabled(); 此方法确定是否 _已选择_ 引用的元素. 此方法广泛用于复选框, 单选按钮, 输入元素和选项元素. -返回一个布尔值, -如果在当前浏览上下文中 **已选择** 引用的元素, +返回一个布尔值, +如果在当前浏览上下文中 **已选择** 引用的元素, 则返回 **True**, 否则返回 **False**. {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes"); - + //returns true if element is checked else returns false boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected(); {{< / code-panel >}} @@ -629,7 +797,7 @@ value = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']:first-of-ty {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/checkboxes"); - + // Returns true if element ins checked else returns false bool value = driver.FindElement(By.CssSelector("input[type='checkbox']:last-of-type")).Selected; {{< / code-panel >}} @@ -648,9 +816,9 @@ await driver.get('https://the-internet.herokuapp.com/checkboxes'); let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type")).isSelected(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://the-internet.herokuapp.com/checkboxes") - + //returns true if element is checked else returns false val attr = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected() {{< / code-panel >}} @@ -664,7 +832,7 @@ let res = await driver.findElement(By.css("input[type='checkbox']:last-of-type") {{< code-tab >}} {{< code-panel language="java" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com"); //returns TagName of the element @@ -680,7 +848,7 @@ attr = driver.find_element(By.CSS_SELECTOR, "h1").tag_name {{< code-panel language="csharp" >}} // Navigate to Url driver.Navigate().GoToUrl("https://www.example.com"); - + // Returns TagName of the element string attr = driver.FindElement(By.CssSelector("h1")).TagName; {{< / code-panel >}} @@ -699,9 +867,9 @@ await driver.get('https://www.example.com'); let value = await driver.findElement(By.css('h1')).getTagName(); {{< / code-panel >}} {{< code-panel language="kotlin" >}} - //navigates to url + //navigates to url driver.get("https://www.example.com") - + //returns TagName of the element val attr = driver.findElement(By.cssSelector("h1")).getTagName() {{< / code-panel >}} @@ -709,7 +877,7 @@ let value = await driver.findElement(By.css('h1')).getTagName(); ## Get Element Rect -用于获取参考元素的尺寸和坐标. +用于获取参考元素的尺寸和坐标. 提取的数据主体包含以下详细信息: * 元素左上角的X轴位置 @@ -731,7 +899,7 @@ System.out.println(res.getX()); {{< code-panel language="python" >}} # Navigate to url driver.get("https://www.example.com") - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(By.CSS_SELECTOR, "h1").rect {{< / code-panel >}} @@ -748,7 +916,7 @@ System.Console.WriteLine(res.Size); {{< code-panel language="ruby" >}} # Navigate to url driver.get 'https://www.example.com' - + # Returns height, width, x and y coordinates referenced element res = driver.find_element(css: "h1").rect {{< / code-panel >}} @@ -815,7 +983,7 @@ cssValue = driver.find_element(:link_text, 'More information...').css_value('col {{< code-panel language="javascript" >}} // Navigate to Url await driver.get('https://www.example.com'); - + // Retrieves the computed style property 'color' of linktext let cssValue = await driver.findElement(By.linkText("More information...")).getCssValue('color'); {{< / code-panel >}} From 3cf46cf25e0b88d977d07b3f7e75672149f73da4 Mon Sep 17 00:00:00 2001 From: AutomatedTester Date: Thu, 25 Feb 2021 14:53:19 +0000 Subject: [PATCH 2/2] Remove relative locator for webelement, clean up imports in locating elements document --- .../locating_elements.de.md | 122 +++++++------ .../locating_elements.en.md | 75 ++++---- .../locating_elements.es.md | 163 +++++++++-------- .../locating_elements.fr.md | 93 ++++++---- .../locating_elements.ja.md | 55 +++--- .../locating_elements.ko.md | 55 +++--- .../locating_elements.nl.md | 79 ++++---- .../locating_elements.pt-br.md | 53 ++++-- .../locating_elements.zh-cn.md | 47 +++-- .../content/webdriver/web_element.de.md | 167 ----------------- .../content/webdriver/web_element.en.md | 162 ----------------- .../content/webdriver/web_element.es.md | 167 ----------------- .../content/webdriver/web_element.fr.md | 167 ----------------- .../content/webdriver/web_element.ja.md | 168 ------------------ .../content/webdriver/web_element.ko.md | 167 ----------------- .../content/webdriver/web_element.nl.md | 161 ----------------- .../content/webdriver/web_element.pt-br.md | 167 ----------------- .../content/webdriver/web_element.zh-cn.md | 167 ----------------- 18 files changed, 439 insertions(+), 1796 deletions(-) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.de.md (90%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.en.md (90%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.es.md (86%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.fr.md (89%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.ja.md (93%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.ko.md (93%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.nl.md (90%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.pt-br.md (93%) rename docs_source_files/content/{getting_started_with_webdriver => webdriver}/locating_elements.zh-cn.md (94%) diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.de.md b/docs_source_files/content/webdriver/locating_elements.de.md similarity index 90% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.de.md rename to docs_source_files/content/webdriver/locating_elements.de.md index 22840c57ebc4..106556589b89 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.de.md +++ b/docs_source_files/content/webdriver/locating_elements.de.md @@ -5,20 +5,20 @@ weight: 3 ### Locating one element -Eine der grundlegendsten Techniken, die bei der Verwendung des WebDriver -erlernt werden müssen, ist wie man Elemente auf der Webseite findet. WebDriver +Eine der grundlegendsten Techniken, die bei der Verwendung des WebDriver +erlernt werden müssen, ist wie man Elemente auf der Webseite findet. WebDriver bietet eine Reihe von verschiedenen Möglichkeiten um Elemente zu finden, darunter die Suche nach einem Element anhand des ID-Attributs: {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("cheese")); +WebElement cheese = driver.findElement(By.id("cheese")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "cheese") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("cheese")); +IWebElement element = driver.FindElement(By.Id("cheese")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -31,7 +31,7 @@ val cheese: WebElement = driver.findElement(By.id("cheese")) {{< / code-panel >}} {{< / code-tab >}} -Wie das Beispiel zeigt, wird die Lokalisierung der Elemente +Wie das Beispiel zeigt, wird die Lokalisierung der Elemente mit dem WebDriver direkt an einer Instanz des `WebDriver` Objektes durchgeführt. Die `findElement(By)` Methode liefert ein Objekt des Types `ẀebElement`. @@ -40,9 +40,9 @@ Types `ẀebElement`. * `WebElement` repräsentiert einen bestimmten DOM Knoten (z.B. einen Link, ein Eingabefeld, etc.) -Ab dem Zeitpunkt, ab dem eine Referenz zu einem WebElement "gefunden" -wurde, kann der Suchumfang auf dieses Element eingeschränkt werden. Es -können weitere eingegrenzte Suchen auf Basis des ausgewählten Elements +Ab dem Zeitpunkt, ab dem eine Referenz zu einem WebElement "gefunden" +wurde, kann der Suchumfang auf dieses Element eingeschränkt werden. Es +können weitere eingegrenzte Suchen auf Basis des ausgewählten Elements durchgeführt werden, indem die gleiche Methode angewandt wird: {{< code-tab >}} @@ -72,25 +72,25 @@ val cheddar = cheese.findElement(By.id("cheddar")) {{< / code-panel >}} {{< / code-tab >}} -Dies wird ermöglicht weil sowohl der _WebDriver_ als auch das +Dies wird ermöglicht weil sowohl der _WebDriver_ als auch das _WebElement_ das Interface [_SearchContext_](//seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/SearchContext.html) -implementieren. Wir verstehen dies im WebDriver als _role-based interface_ +implementieren. Wir verstehen dies im WebDriver als _role-based interface_ (rollenbasiertes Interface). Dieses Interface ermöglicht um herauszufinden ob eine driver Implementierung ein bestimmtes Feature unterstützt oder nicht. Diese Schnittstellen (Interface) sind klar definiert und versuchen daran festzuhalten, dass -es nur eine Verantwortlichkeit dafür gibt. Mehr über den Aufbau und die +es nur eine Verantwortlichkeit dafür gibt. Mehr über den Aufbau und die Verantwortlichkeiten der Driver können hier nachgelesen werden [Link zu einer Sektion die noch definiert werden muss](#) - + Folglich untersützt das _By_ Interface zahlreich zusätzliche Suchstrategien. -Eine verschachtelte Suche ist nicht die effektivste Methode um die den gewünschten -Käse zu finden. Es werden zwei getrennte Befehle an den Browser gesendet. Der -erste der den gesamten DOM nach dem Element mit der ID "cheese" sucht, gefolgt +Eine verschachtelte Suche ist nicht die effektivste Methode um die den gewünschten +Käse zu finden. Es werden zwei getrennte Befehle an den Browser gesendet. Der +erste der den gesamten DOM nach dem Element mit der ID "cheese" sucht, gefolgt von der Suche nach "cheddar" mit einem eingeschränkten Kontext. -Um die Effektivität zu erhöhen sollte ein präziserer Locator (Identifizierungsstrategie) -gewählt werden; WebDriver unterstützt die Suche nach Elementen auch mit Hilfe eines -CSS-locators, mit dem es auch möglich ist Kombinationen in einer einzelnen Suche +Um die Effektivität zu erhöhen sollte ein präziserer Locator (Identifizierungsstrategie) +gewählt werden; WebDriver unterstützt die Suche nach Elementen auch mit Hilfe eines +CSS-locators, mit dem es auch möglich ist Kombinationen in einer einzelnen Suche durchzuführen: {{< code-tab >}} @@ -128,11 +128,11 @@ Liste mit Käsesorten die uns am besten schmecken: ``` -Es steht außer Frage, je mehr Käse desto besser, es wäre aber umständlich +Es steht außer Frage, je mehr Käse desto besser, es wäre aber umständlich jedes Element einzeln abrufen zu müssen. Daher gibt es die Möglichkeit -mit `findElements(By)` mehrere Elemente gleichzeitig zu finden. Diese Methode liefert +mit `findElements(By)` mehrere Elemente gleichzeitig zu finden. Diese Methode liefert eine Sammlung (Collection) von WebElementen. Wird nur ein Element gefunden, wird trotzdem -eine Sammlung (mit einem Element) retourniert. Wird kein Element gefunden +eine Sammlung (mit einem Element) retourniert. Wird kein Element gefunden ist die Liste leer. {{< code-tab >}} @@ -174,34 +174,34 @@ Im WebDriver existieren acht unterschiedliche Möglichkeiten um Elemente zu loka ### Tips zur Verwendung von Selektoren Die bevorzugte Methode um Elemente zu identifizieren ist mit Sicherheit -mit Hilfe der HTML IDs. Diese sind eindeutig, konsitent und vorhersehbar, +mit Hilfe der HTML IDs. Diese sind eindeutig, konsitent und vorhersehbar, weiters arbeitet diese Methode sehr schnell, da hierbei auf komplizierte DOM Verarbeitungen verzichtet wird. Wenn eindeutige IDs nicht verfügbar sind, ist ein gut definierter -CSS selector die bevorzugte Methode um Elemente zu lokalisieren. +CSS selector die bevorzugte Methode um Elemente zu lokalisieren. XPath-Suchen funktionieren gleich dem CSS-Selektoren, allerdings ist die -Syntax komplizierter und schwieriger zu debuggen. Obwohl XPath-Selektoren -sehr flexibel sind, sind sie in der Regel nicht von den Browser-Herstellern +Syntax komplizierter und schwieriger zu debuggen. Obwohl XPath-Selektoren +sehr flexibel sind, sind sie in der Regel nicht von den Browser-Herstellern auf Leistung getestet und sind tendenziell recht langsam. -Selektorstrategien die _linkText_ oder _partialLinkText_ verwenden -haben den Nachteil das diese nur für Link-Elemente angewandt werden -können. Weiters werden diese Selektoren intern im WebDriver als +Selektorstrategien die _linkText_ oder _partialLinkText_ verwenden +haben den Nachteil das diese nur für Link-Elemente angewandt werden +können. Weiters werden diese Selektoren intern im WebDriver als XPath-Selektoren aufgelöst. -Den HTML-Tag als Identifizierungsmerkmal zu verwenden kann gefährlich +Den HTML-Tag als Identifizierungsmerkmal zu verwenden kann gefährlich sein. Meistens sind viele Elemente mit dem gleichen HTML-Tag auf einer Webseite. Eine sinnvolle Verwendung könnte sein, diese Strategie mit der _findElements(By)_ Methode zu verwenden, die eine Sammlung von WebElementen retourniert. -Empfohlen wird die Suchen so kompackt und einfach lesbar wie möglich zu halten. -Den DOM abzufragen ist eine aufwändige Operation für den WebDriver, +Empfohlen wird die Suchen so kompackt und einfach lesbar wie möglich zu halten. +Den DOM abzufragen ist eine aufwändige Operation für den WebDriver, und je präziser der Suchbegriff ist, desto besser. ## Relative Suchstrategien -**Selenium 4** führt relative Locators ein, die zuvor als +**Selenium 4** führt relative Locators ein, die zuvor als _Friendly Locators_ bekannt waren. Diese Funktionalität wurde hinzugefügt um Elemente zu finden, die sicht in der Nähe zu anderen Elementen befinden. @@ -216,10 +216,10 @@ Folgende relative Locators sind verfügbar: _findElement_ Methode akzeptiert nun eine weitere Möglichkeit `withTagName()` die einen relativen Locator liefert. -_findElement_ method now accepts a new method `withTagName()` -which returns a RelativeLocator. +_findElement_ method now accepts a new method `withTagName()` +which returns a RelativeLocator. -### Wie funktionieren die relativen Suchemethoden +### Wie funktionieren die relativen Suchemethoden Selenium verwendet folgende JavaScript Funktion [getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) um das entsprechende Element zu finden. Diese @@ -237,18 +237,22 @@ Liefert das WebElement, welches sich über dem spezifiziertem Element befindet. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) + {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -269,23 +273,26 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi ### below() - unterhalb -Findet das WebElement, welches sich unter dem +Findet das WebElement, welches sich unter dem spezifiziertem Element befindet. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -311,19 +318,22 @@ befindet. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") - .toLeftOf(submitButton)); + .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -349,19 +359,22 @@ Liefert das WebElement, das sich rechts des spezifierten Elements befindet. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -382,24 +395,27 @@ val submitButton= driver.findElement(withTagName("button").toRightOf(cancelButto ### near() - in der Nähe von -Liefert das WebElement, welches maximal `50px` vom spezifizierten +Liefert das WebElement, welches maximal `50px` vom spezifizierten Element entfernt ist. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.en.md b/docs_source_files/content/webdriver/locating_elements.en.md similarity index 90% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.en.md rename to docs_source_files/content/webdriver/locating_elements.en.md index 0bcb3307705e..56af8dada4c7 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.en.md +++ b/docs_source_files/content/webdriver/locating_elements.en.md @@ -11,13 +11,13 @@ types, amongst them finding an element by its ID attribute: {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("cheese")); +WebElement cheese = driver.findElement(By.id("cheese")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "cheese") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("cheese")); +IWebElement element = driver.FindElement(By.Id("cheese")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -200,8 +200,8 @@ your search, the better. ## Relative Locators -**Selenium 4** brings Relative Locators which are previously -called as _Friendly Locators_. This functionality was +**Selenium 4** brings Relative Locators which are previously +called as _Friendly Locators_. This functionality was added to help you locate elements that are nearby other elements. The Available Relative Locators are: @@ -211,15 +211,15 @@ The Available Relative Locators are: * *toRightOf* * *near* -_findElement_ method now accepts a new method `withTagName()` -which returns a RelativeLocator. +_findElement_ method now accepts a new method `withTagName()` +which returns a RelativeLocator. ### How does it work -Selenium uses the JavaScript function +Selenium uses the JavaScript function [getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) -to find the relative elements. This function returns -properties of an element such as +to find the relative elements. This function returns +properties of an element such as right, left, bottom, and top. Let us consider the below example for understanding the relative locators. @@ -228,23 +228,26 @@ Let us consider the below example for understanding the relative locators. ### above() -Returns the WebElement, which appears +Returns the WebElement, which appears above to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -266,23 +269,26 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi ### below() -Returns the WebElement, which appears +Returns the WebElement, which appears below to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -304,24 +310,27 @@ val passwordField = driver.findElement(withTagName("input").below(emailAddressFi ### toLeftOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to left of specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") - .toLeftOf(submitButton)); + .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -343,24 +352,27 @@ val cancelButton= driver.findElement(withTagName("button").toLeftOf(submitButton ### toRightOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to right of the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -386,19 +398,22 @@ at most `50px` away from the specified element. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name -emailAddressLabel = driver.find_element(By.ID, "lbl-email") +from selenium.webdriver.support.relative_locator import with_tag_name + +emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.es.md b/docs_source_files/content/webdriver/locating_elements.es.md similarity index 86% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.es.md rename to docs_source_files/content/webdriver/locating_elements.es.md index 003463a9d8d8..c315b583decf 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.es.md +++ b/docs_source_files/content/webdriver/locating_elements.es.md @@ -5,20 +5,20 @@ weight: 3 ### Localizando un elemento -Una de las técnicas más fundamentales para aprender al usar WebDriver -es cómo encontrar elementos en la página. WebDriver ofrece varios -tipos de selectores integrados, entre ellos encontrar un elemento por +Una de las técnicas más fundamentales para aprender al usar WebDriver +es cómo encontrar elementos en la página. WebDriver ofrece varios +tipos de selectores integrados, entre ellos encontrar un elemento por su atributo ID: {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("cheese")); +WebElement cheese = driver.findElement(By.id("cheese")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "cheese") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("cheese")); +IWebElement element = driver.FindElement(By.Id("cheese")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -31,18 +31,18 @@ val cheese: WebElement = driver.findElement(By.id("cheese")) {{< / code-panel >}} {{< / code-tab >}} -Como se ve en el ejemplo, localizar elementos en WebDriver +Como se ve en el ejemplo, localizar elementos en WebDriver se realiza en la instancia del objeto `WebDriver`. El método -`findElement(By)` devuelve otro tipo de objeto fundamental, -el `WebElement`. +`findElement(By)` devuelve otro tipo de objeto fundamental, +el `WebElement`. -* `WebDriver` representa el navegador -* `WebElement` representa un -nodo particular del DOM (un control, por ejemplo un enlace o campo de -entrada, etc.) +* `WebDriver` representa el navegador +* `WebElement` representa un +nodo particular del DOM (un control, por ejemplo un enlace o campo de +entrada, etc.) -Una vez que tengas una referencia a un elemento web que se ha -"encontrado", puedes reducir el alcance de tu búsqueda utilizando la +Una vez que tengas una referencia a un elemento web que se ha +"encontrado", puedes reducir el alcance de tu búsqueda utilizando la misma llamada en la instancia de ese objeto: {{< code-tab >}} @@ -76,14 +76,14 @@ Puedes hacer esto porque los tipos _WebDriver_ y _WebElement_ implementan la interfaz [_SearchContext_](//seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/SearchContext.html). En WebDriver, esto se conoce como _interfaz basada en roles_. Las interfaces basadas en roles te permiten determinar si -la implementación del controlador admite una característica dada. +la implementación del controlador admite una característica dada. Estas interfaces están claramente definidas y tratan de cumplir con tener un solo rol de responsabilidad. Puede leer más sobre el diseño de WebDriver y qué roles son compatibles con qué controladores en [Otra sección](#). En consecuencia, la interfaz _By_ utilizada anteriormente también permite una serie de -estrategias adicionales de localización. Una búsqueda anidada podría no ser la +estrategias adicionales de localización. Una búsqueda anidada podría no ser la estrategia mas efectiva para localizar _cheese_ ya que requiere dos comandos que se emitirán al navegador; primero buscando en el DOM un elemento con ID "cheese", luego una búsqueda de "cheddar" en un contexto reducido. @@ -127,9 +127,9 @@ una lista ordenada del queso que más nos gusta: ``` Dado que más queso es indiscutiblemente mejor, y sería engorroso -tener que recuperar cada uno de los elementos individualmente, -una técnica superior para recuperar _cheese_ es hacer uso de la -versión pluralizada `findElements(By)`. Este método devuelve una +tener que recuperar cada uno de los elementos individualmente, +una técnica superior para recuperar _cheese_ es hacer uso de la +versión pluralizada `findElements(By)`. Este método devuelve una colección de elementos web. Si solo se encuentra un elemento, aún devolverá una colección (de un elemento). Si ningún elemento coincide con el localizador, se devolverá la lista vacía. @@ -171,38 +171,38 @@ Hay ocho estrategias diferentes de ubicación de elementos integradas en WebDriv ### Consejos sobre el uso de selectores -En general, si los ID del HTML están disponibles, son únicos y -consistentemente predecibles, son el método preferido para ubicar un -elemento en una página. Tienden a trabajar muy rápido y renuncian al -mucho procesamiento que viene con recorridos DOM complicados. - -Si las ID únicas no están disponibles, un selector CSS bien escrito -es el método preferido para localizar un elemento. XPath funciona tan -bien como los selectores CSS, pero la sintaxis es complicada y con -frecuencia difícil de depurar. Aunque los selectores XPath son muy -flexibles, generalmente su desempeño no es probado por lo proveedores -de navegadores y tienden a ser bastante lentos. - -Las estrategias de selección basadas en enlaces de texto y enlaces de -texto parciales tienen el inconveniente en que solo funcionan en -elementos de enlace. Además, internamente en WebDriver llaman a los -selectores XPath. - -El nombre de la etiqueta puede ser una forma peligrosa de localizar -elementos. Existen frecuentemente múltiples elementos con la misma -etiqueta presentes en la página. Esto es mayormente útil cuando se -llama al método _findElements(By)_ que devuelve una colección de -elementos. - -La recomendación es mantener tus localizadores tan compactos y -legibles como sea posible. Pedirle a WebDriver que atraviese la -estructura del DOM es una operación costosa, y cuanto más se pueda +En general, si los ID del HTML están disponibles, son únicos y +consistentemente predecibles, son el método preferido para ubicar un +elemento en una página. Tienden a trabajar muy rápido y renuncian al +mucho procesamiento que viene con recorridos DOM complicados. + +Si las ID únicas no están disponibles, un selector CSS bien escrito +es el método preferido para localizar un elemento. XPath funciona tan +bien como los selectores CSS, pero la sintaxis es complicada y con +frecuencia difícil de depurar. Aunque los selectores XPath son muy +flexibles, generalmente su desempeño no es probado por lo proveedores +de navegadores y tienden a ser bastante lentos. + +Las estrategias de selección basadas en enlaces de texto y enlaces de +texto parciales tienen el inconveniente en que solo funcionan en +elementos de enlace. Además, internamente en WebDriver llaman a los +selectores XPath. + +El nombre de la etiqueta puede ser una forma peligrosa de localizar +elementos. Existen frecuentemente múltiples elementos con la misma +etiqueta presentes en la página. Esto es mayormente útil cuando se +llama al método _findElements(By)_ que devuelve una colección de +elementos. + +La recomendación es mantener tus localizadores tan compactos y +legibles como sea posible. Pedirle a WebDriver que atraviese la +estructura del DOM es una operación costosa, y cuanto más se pueda reducir el alcance de tu búsqueda, mejor. ## Relative Locators -**Selenium 4** brings Relative Locators which are previously -called as _Friendly Locators_. This functionality was +**Selenium 4** brings Relative Locators which are previously +called as _Friendly Locators_. This functionality was added to help you locate elements that are nearby other elements. The Available Relative Locators are: @@ -212,15 +212,15 @@ The Available Relative Locators are: * *toRightOf* * *near* -_findElement_ method now accepts a new method `withTagName()` -which returns a RelativeLocator. +_findElement_ method now accepts a new method `withTagName()` +which returns a RelativeLocator. ### How does it work -Selenium uses the JavaScript function +Selenium uses the JavaScript function [getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) -to find the relative elements. This function returns -properties of an element such as +to find the relative elements. This function returns +properties of an element such as right, left, bottom, and top. Let us consider the below example for understanding the relative locators. @@ -229,23 +229,26 @@ Let us consider the below example for understanding the relative locators. ### above() -Returns the WebElement, which appears +Returns the WebElement, which appears above to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -267,23 +270,26 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi ### below() -Returns the WebElement, which appears +Returns the WebElement, which appears below to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -305,24 +311,27 @@ val passwordField = driver.findElement(withTagName("input").below(emailAddressFi ### toLeftOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to left of specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") - .toLeftOf(submitButton)); + .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -344,24 +353,27 @@ val cancelButton= driver.findElement(withTagName("button").toLeftOf(submitButton ### toRightOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to right of the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -387,19 +399,22 @@ at most `50px` away from the specified element. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name -emailAddressLabel = driver.find_element(By.ID, "lbl-email") +from selenium.webdriver.support.relative_locator import with_tag_name + +emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); @@ -416,4 +431,4 @@ let emailAddressField = await driver.findElements(withTagName("input").near(emai val emailAddressLabel = driver.findElement(By.id("lbl-email")) val emailAddressField = driver.findElement(withTagName("input").near(emailAddressLabel)) {{< / code-panel >}} -{{< / code-tab >}} \ No newline at end of file +{{< / code-tab >}} diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.fr.md b/docs_source_files/content/webdriver/locating_elements.fr.md similarity index 89% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.fr.md rename to docs_source_files/content/webdriver/locating_elements.fr.md index d29ee26b4f66..1af30befc1c8 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.fr.md +++ b/docs_source_files/content/webdriver/locating_elements.fr.md @@ -5,20 +5,20 @@ weight: 3 ### Localiser des éléments -Une des techniques fondamentales à maîtriser lorsque l'on utilise WebDriver -consiste à chercher des éléments sur une page. +Une des techniques fondamentales à maîtriser lorsque l'on utilise WebDriver +consiste à chercher des éléments sur une page. WebDriver offre pour cela un ensemble pré-défini de type de selecteurs, parmi lesquels la recherche d'une élément par son attribut ID: {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("fromage")); +WebElement cheese = driver.findElement(By.id("fromage")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "fromage") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("fromage")); +IWebElement element = driver.FindElement(By.Id("fromage")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -32,7 +32,7 @@ val cheese: WebElement = driver.findElement(By.id("cheese")) {{< / code-tab >}} Comme démontré dans cet exemple, la localisation des éléments à l'aide de WebDriver -se fait via une instance de l'objet `WebDriver`. +se fait via une instance de l'objet `WebDriver`. La méthode `findElement(By)` retourne un autre type d'objet fondamental, un `WebElement`. * `WebDriver` represente la navigateur @@ -80,14 +80,14 @@ Vous pouvez en lire plus sur le design de WebDriver et sur quels drivers support Par conséquent, l'interface _By_ utilisée précédement fournit également -d'autres stratégies de localisation. Une recherche imbriquée peut ne pas -être la startégie la plus adaptée pour trouver notre cheddar +d'autres stratégies de localisation. Une recherche imbriquée peut ne pas +être la startégie la plus adaptée pour trouver notre cheddar puisqu'elle nécessite que deux instructions séparées soient envoyées au navigateur ; -tout d'abord rechercher un élément ayant pour ID "fromage", +tout d'abord rechercher un élément ayant pour ID "fromage", puis une recherche pour "cheddar" dans ce contexte plus restreint. Pour améliorer légèrement les performances, nous pourrions essayer -un sélecteur (une stratégie de localisation) plus spécifique : +un sélecteur (une stratégie de localisation) plus spécifique : WebDriver supporte la localisation d'élément via sélecteur CSS, nous permettant de combiner les deux sélecteurs précédents en un seul: @@ -183,11 +183,11 @@ ils sont rarement testés d'un point de vue performance par les fournisseurs de et ont donc tendance à être assez lents. Les stratégies basés sur _linkText_ et _partialLinkText_ sont -contraingnantes du fait qu'elles ne fonctionnent +contraingnantes du fait qu'elles ne fonctionnent que sur des éléments de type lien hypertexte. De plus, elles sont implémentées au sein de WebDriver via des sélecteurs XPath. -Le nom de tag est une façon dangereuse de localiser des éléments. +Le nom de tag est une façon dangereuse de localiser des éléments. Il y a fréquemment de multiples éléments ayant le même tag sur une page. Cette stratégie est principalement utile lorsque utilisée avec la méthode _findElements(By)_, renvoyant une collection des élements. @@ -198,8 +198,8 @@ de fait plus le scope de recherche sera restreint, meilleures seront les perform ## Relative Locators -**Selenium 4** brings Relative Locators which are previously -called as _Friendly Locators_. This functionality was +**Selenium 4** brings Relative Locators which are previously +called as _Friendly Locators_. This functionality was added to help you locate elements that are nearby other elements. The Available Relative Locators are: @@ -209,15 +209,15 @@ The Available Relative Locators are: * *toRightOf* * *near* -_findElement_ method now accepts a new method `withTagName()` -which returns a RelativeLocator. +_findElement_ method now accepts a new method `withTagName()` +which returns a RelativeLocator. ### How does it work -Selenium uses the JavaScript function +Selenium uses the JavaScript function [getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) -to find the relative elements. This function returns -properties of an element such as +to find the relative elements. This function returns +properties of an element such as right, left, bottom, and top. Let us consider the below example for understanding the relative locators. @@ -226,23 +226,26 @@ Let us consider the below example for understanding the relative locators. ### above() -Returns the WebElement, which appears +Returns the WebElement, which appears above to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -264,23 +267,26 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi ### below() -Returns the WebElement, which appears +Returns the WebElement, which appears below to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -302,24 +308,27 @@ val passwordField = driver.findElement(withTagName("input").below(emailAddressFi ### toLeftOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to left of specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -341,24 +350,27 @@ val cancelButton= driver.findElement(withTagName("button").toLeftOf(submitButton ### toRightOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to right of the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -384,19 +396,22 @@ at most `50px` away from the specified element. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name -emailAddressLabel = driver.find_element(By.ID, "lbl-email") +from selenium.webdriver.support.relative_locator import with_tag_name + +emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); @@ -413,4 +428,4 @@ let emailAddressField = await driver.findElements(withTagName("input").near(emai val emailAddressLabel = driver.findElement(By.id("lbl-email")) val emailAddressField = driver.findElement(withTagName("input").near(emailAddressLabel)) {{< / code-panel >}} -{{< / code-tab >}} \ No newline at end of file +{{< / code-tab >}} diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.ja.md b/docs_source_files/content/webdriver/locating_elements.ja.md similarity index 93% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.ja.md rename to docs_source_files/content/webdriver/locating_elements.ja.md index ae449f72cb07..11dad3da648b 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.ja.md +++ b/docs_source_files/content/webdriver/locating_elements.ja.md @@ -10,13 +10,13 @@ weight: 3 {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("cheese")); +WebElement cheese = driver.findElement(By.id("cheese")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "cheese") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("cheese")); +IWebElement element = driver.FindElement(By.Id("cheese")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -201,18 +201,21 @@ Seleniumは、JavaScript関数 [getBoundingClientRect()](https://developer.mozil {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -238,18 +241,21 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -275,19 +281,22 @@ val passwordField = driver.findElement(withTagName("input").below(emailAddressFi {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") - .toLeftOf(submitButton)); + .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -313,19 +322,22 @@ val cancelButton= driver.findElement(withTagName("button").toLeftOf(submitButton {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -350,19 +362,22 @@ val submitButton= driver.findElement(withTagName("button").toRightOf(cancelButto {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name -emailAddressLabel = driver.find_element(By.ID, "lbl-email") +from selenium.webdriver.support.relative_locator import with_tag_name + +emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); @@ -379,4 +394,4 @@ let emailAddressField = await driver.findElements(withTagName("input").near(emai val emailAddressLabel = driver.findElement(By.id("lbl-email")) val emailAddressField = driver.findElement(withTagName("input").near(emailAddressLabel)) {{< / code-panel >}} -{{< / code-tab >}} \ No newline at end of file +{{< / code-tab >}} diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.ko.md b/docs_source_files/content/webdriver/locating_elements.ko.md similarity index 93% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.ko.md rename to docs_source_files/content/webdriver/locating_elements.ko.md index 8d9ce6c4a0be..e4e076d2cece 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.ko.md +++ b/docs_source_files/content/webdriver/locating_elements.ko.md @@ -9,13 +9,13 @@ weight: 3 {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("cheese")); +WebElement cheese = driver.findElement(By.id("cheese")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "cheese") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("cheese")); +IWebElement element = driver.FindElement(By.Id("cheese")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -65,7 +65,7 @@ val cheddar = cheese.findElement(By.id("cheddar")) 이것이 가능한 이유는 *WebDriver* 과 *WebElement* 모두 [_SearchContext_](//seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/SearchContext.html)라는 인터페이스를 구현한 것이기 때문입니다.이것은 웹드라이버에서 role-based(역할 기반) 인터페이스라고 알려져 있습니다. Role-based 인터페이스는 특정 드라이버 구현이 주어진 기능을 지원하는지 확인할 수 있게 해줍니다. 이러한 인터페이스는 명확하게 정의되어 있으며, 단 하나의 역할만을 가지고 있는 것을 고수하려고 노력합니다. 웹드라이버의 디자인이나 어떠한 드라이버에서 어떠한 역할을 지원하는지에 대해서는 [여기(아직 미구현)](#)를 참고하십시오. -따라서, 위에서 사용한 *By*인터페이스도 다양한 추가 locator 전략을 지원합니다. 중첩된 조회는 브라우저에 별도의 두 개 명령을 실행해야하므로 가장 효과적인 cheese 찾기 전략이 아닐 수 있습니다. 별도의 두 개 명령은 ID "cheese"를 DOM에서 찾는 명령과 그 곳에서 "cheddar"를 그 다음에 찾는 명령을 말합니다. +따라서, 위에서 사용한 *By*인터페이스도 다양한 추가 locator 전략을 지원합니다. 중첩된 조회는 브라우저에 별도의 두 개 명령을 실행해야하므로 가장 효과적인 cheese 찾기 전략이 아닐 수 있습니다. 별도의 두 개 명령은 ID "cheese"를 DOM에서 찾는 명령과 그 곳에서 "cheddar"를 그 다음에 찾는 명령을 말합니다. 성능을 향상시키기 위해서는 좀 더 세부적인 locator를 사용해야합니다. 웹드라이버는 CSS locator를 통해 element를 검색하는 기능을 지원하고, 이는 위처럼 두 개의 locator를 사용할 필요가 없게 해줍니다: @@ -181,18 +181,21 @@ _findElement_ 메소드는 또다른 메소드 `withTagName()` 를 받습니다. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -218,18 +221,21 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -255,19 +261,22 @@ val passwordField = driver.findElement(withTagName("input").below(emailAddressFi {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -293,19 +302,22 @@ val cancelButton= driver.findElement(withTagName("button").toLeftOf(submitButton {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -330,19 +342,22 @@ val submitButton= driver.findElement(withTagName("button").toRightOf(cancelButto {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name -emailAddressLabel = driver.find_element(By.ID, "lbl-email") +from selenium.webdriver.support.relative_locator import with_tag_name + +emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); @@ -359,4 +374,4 @@ let emailAddressField = await driver.findElements(withTagName("input").near(emai val emailAddressLabel = driver.findElement(By.id("lbl-email")) val emailAddressField = driver.findElement(withTagName("input").near(emailAddressLabel)) {{< / code-panel >}} -{{< / code-tab >}} \ No newline at end of file +{{< / code-tab >}} diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.nl.md b/docs_source_files/content/webdriver/locating_elements.nl.md similarity index 90% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.nl.md rename to docs_source_files/content/webdriver/locating_elements.nl.md index 64932741c53d..3739de9da699 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.nl.md +++ b/docs_source_files/content/webdriver/locating_elements.nl.md @@ -5,7 +5,7 @@ weight: 3 {{% notice info %}} ### Localisatie van elementen - Page being translated from + Page being translated from English to Dutch. Do you speak Dutch? Help us to translate it by sending us pull requests! {{% /notice %}} @@ -19,13 +19,13 @@ is het lokaliseren van een element op basis van zijn ID: {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("cheese")); +WebElement cheese = driver.findElement(By.id("cheese")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "cheese") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("cheese")); +IWebElement element = driver.FindElement(By.Id("cheese")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -209,8 +209,8 @@ your search, the better. ## Relative Locators -**Selenium 4** brings Relative Locators which are previously -called as _Friendly Locators_. This functionality was +**Selenium 4** brings Relative Locators which are previously +called as _Friendly Locators_. This functionality was added to help you locate elements that are nearby other elements. The Available Relative Locators are: @@ -220,15 +220,15 @@ The Available Relative Locators are: * *toRightOf* * *near* -_findElement_ method now accepts a new method `withTagName()` -which returns a RelativeLocator. +_findElement_ method now accepts a new method `withTagName()` +which returns a RelativeLocator. ### How does it work -Selenium uses the JavaScript function +Selenium uses the JavaScript function [getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) -to find the relative elements. This function returns -properties of an element such as +to find the relative elements. This function returns +properties of an element such as right, left, bottom, and top. Let us consider the below example for understanding the relative locators. @@ -237,23 +237,26 @@ Let us consider the below example for understanding the relative locators. ### above() -Returns the WebElement, which appears +Returns the WebElement, which appears above to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -275,23 +278,26 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi ### below() -Returns the WebElement, which appears +Returns the WebElement, which appears below to the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -313,24 +319,27 @@ val passwordField = driver.findElement(withTagName("input").below(emailAddressFi ### toLeftOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to left of specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") - .toLeftOf(submitButton)); + .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -352,24 +361,27 @@ val cancelButton= driver.findElement(withTagName("button").toLeftOf(submitButton ### toRightOf() -Returns the WebElement, which appears +Returns the WebElement, which appears to right of the specified element {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -395,19 +407,22 @@ at most `50px` away from the specified element. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name -emailAddressLabel = driver.find_element(By.ID, "lbl-email") +from selenium.webdriver.support.relative_locator import with_tag_name + +emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); @@ -424,4 +439,4 @@ let emailAddressField = await driver.findElements(withTagName("input").near(emai val emailAddressLabel = driver.findElement(By.id("lbl-email")) val emailAddressField = driver.findElement(withTagName("input").near(emailAddressLabel)) {{< / code-panel >}} -{{< / code-tab >}} \ No newline at end of file +{{< / code-tab >}} diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.pt-br.md b/docs_source_files/content/webdriver/locating_elements.pt-br.md similarity index 93% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.pt-br.md rename to docs_source_files/content/webdriver/locating_elements.pt-br.md index 17d2d2abf198..80d688f29f48 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.pt-br.md +++ b/docs_source_files/content/webdriver/locating_elements.pt-br.md @@ -11,13 +11,13 @@ entre eles encontrar um elemento por seu atributo de ID: {{< code-tab >}} {{< code-panel language="java" >}} -WebElement cheese = driver.findElement(By.id("cheese")); +WebElement cheese = driver.findElement(By.id("cheese")); {{< / code-panel >}} {{< code-panel language="python" >}} driver.find_element(By.ID, "cheese") {{< / code-panel >}} {{< code-panel language="csharp" >}} -IWebElement element = driver.FindElement(By.Id("cheese")); +IWebElement element = driver.FindElement(By.Id("cheese")); {{< / code-panel >}} {{< code-panel language="ruby" >}} cheese = driver.find_element(id: 'cheese') @@ -230,18 +230,21 @@ acima do elemento especificado {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField = driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -268,18 +271,21 @@ abaixo do elemento especificado {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField = driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -306,19 +312,22 @@ Retorna o WebElement, que aparece {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton = driver.findElement(By.id("submit")); WebElement cancelButton = driver.findElement(withTagName("button") - .toLeftOf(submitButton)); + .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -345,19 +354,22 @@ Retorna o WebElement, que aparece {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton = driver.findElement(By.id("cancel")); WebElement submitButton = driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -383,19 +395,22 @@ no máximo a `50px` de distância do elemento especificado. {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel = driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name -emailAddressLabel = driver.find_element(By.ID, "lbl-email") +from selenium.webdriver.support.relative_locator import with_tag_name + +emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); diff --git a/docs_source_files/content/getting_started_with_webdriver/locating_elements.zh-cn.md b/docs_source_files/content/webdriver/locating_elements.zh-cn.md similarity index 94% rename from docs_source_files/content/getting_started_with_webdriver/locating_elements.zh-cn.md rename to docs_source_files/content/webdriver/locating_elements.zh-cn.md index 2dc2bdc79444..d66658ab645f 100644 --- a/docs_source_files/content/getting_started_with_webdriver/locating_elements.zh-cn.md +++ b/docs_source_files/content/webdriver/locating_elements.zh-cn.md @@ -185,18 +185,21 @@ Selenium是通过使用JavaScript函数 {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement passwordField= driver.findElement(By.id("password")); WebElement emailAddressField = driver.findElement(withTagName("input") .above(passwordField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + passwordField = driver.find_element(By.ID, "password") emailAddressField = driver.find_element(with_tag_name("input").above(passwordField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement passwordField = driver.FindElement(By.Id("password")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                    .Above(passwordField)); @@ -223,18 +226,21 @@ val emailAddressField = driver.findElement(withTagName("input").above(passwordFi {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressField= driver.findElement(By.id("email")); WebElement passwordField = driver.findElement(withTagName("input") .below(emailAddressField)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressField = driver.find_element(By.ID, "email") passwordField = driver.find_element(with_tag_name("input").below(emailAddressField)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy;   +using static OpenQA.Selenium.RelativeBy;   + IWebElement emailAddressField = driver.FindElement(By.Id("email")); IWebElement passwordField = driver.FindElement(WithTagName("input")                                               .Below(emailAddressField)); @@ -260,19 +266,22 @@ val passwordField = driver.findElement(withTagName("input").below(emailAddressFi {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement submitButton= driver.findElement(By.id("submit")); WebElement cancelButton= driver.findElement(withTagName("button") - .toLeftOf(submitButton)); + .toLeftOf(submitButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + submitButton = driver.find_element(By.ID, "submit") cancelButton = driver.find_element(with_tag_name("button"). to_left_of(submitButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement submitButton = driver.FindElement(By.Id("submit")); IWebElement cancelButton = driver.FindElement(WithTagName("button")                                               .LeftOf(submitButton)); @@ -298,19 +307,22 @@ val cancelButton= driver.findElement(withTagName("button").toLeftOf(submitButton {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement cancelButton= driver.findElement(By.id("cancel")); WebElement submitButton= driver.findElement(withTagName("button") .toRightOf(cancelButton)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + cancelButton = driver.find_element(By.ID, "cancel") submitButton = driver.find_element(with_tag_name("button"). to_right_of(cancelButton)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement cancelButton = driver.FindElement(By.Id("cancel")); IWebElement submitButton = driver.FindElement(WithTagName("button")                                               .RightOf(cancelButton)); @@ -335,19 +347,22 @@ val submitButton= driver.findElement(withTagName("button").toRightOf(cancelButto {{< code-tab >}} {{< code-panel language="java" >}} -//import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; +import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; + WebElement emailAddressLabel= driver.findElement(By.id("lbl-email")); WebElement emailAddressField = driver.findElement(withTagName("input") .near(emailAddressLabel)); {{< / code-panel >}} {{< code-panel language="python" >}} -#from selenium.webdriver.support.relative_locator import with_tag_name +from selenium.webdriver.support.relative_locator import with_tag_name + emailAddressLabel = driver.find_element(By.ID, "lbl-email") emailAddressField = driver.find_element(with_tag_name("input"). near(emailAddressLabel)) {{< / code-panel >}} {{< code-panel language="csharp" >}} -//using static OpenQA.Selenium.RelativeBy; +using static OpenQA.Selenium.RelativeBy; + IWebElement emailAddressLabel = driver.FindElement(By.Id("lbl-email")); IWebElement emailAddressField = driver.FindElement(WithTagName("input")                                                   .Near(emailAddressLabel)); diff --git a/docs_source_files/content/webdriver/web_element.de.md b/docs_source_files/content/webdriver/web_element.de.md index 37764596ca54..1cced5ff84b3 100644 --- a/docs_source_files/content/webdriver/web_element.de.md +++ b/docs_source_files/content/webdriver/web_element.de.md @@ -222,173 +222,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -{{% notice info %}} - Page being translated from -English. Do you speak German? Help us to translate -it by sending us pull requests! -{{% /notice %}} - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} ## Finden eines Elements innerhalb anderer Elemente diff --git a/docs_source_files/content/webdriver/web_element.en.md b/docs_source_files/content/webdriver/web_element.en.md index 56b3dd242746..305d3a51e611 100644 --- a/docs_source_files/content/webdriver/web_element.en.md +++ b/docs_source_files/content/webdriver/web_element.en.md @@ -220,168 +220,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} - ## Find Element From Element diff --git a/docs_source_files/content/webdriver/web_element.es.md b/docs_source_files/content/webdriver/web_element.es.md index b6126dcead61..5c0f112e2ed6 100644 --- a/docs_source_files/content/webdriver/web_element.es.md +++ b/docs_source_files/content/webdriver/web_element.es.md @@ -225,173 +225,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -{{% notice info %}} - Page being translated from -English. Do you speak Spanish? Help us to translate -it by sending us pull requests! -{{% /notice %}} - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} ## Find Element desde Element diff --git a/docs_source_files/content/webdriver/web_element.fr.md b/docs_source_files/content/webdriver/web_element.fr.md index c7d98a90495d..85f824888cdf 100644 --- a/docs_source_files/content/webdriver/web_element.fr.md +++ b/docs_source_files/content/webdriver/web_element.fr.md @@ -220,173 +220,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -{{% notice info %}} - Page being translated from -English. Do you speak French? Help us to translate -it by sending us pull requests! -{{% /notice %}} - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} ## Find Element From Element diff --git a/docs_source_files/content/webdriver/web_element.ja.md b/docs_source_files/content/webdriver/web_element.ja.md index 5b8a3ea4bf95..f8c809530ade 100644 --- a/docs_source_files/content/webdriver/web_element.ja.md +++ b/docs_source_files/content/webdriver/web_element.ja.md @@ -218,174 +218,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -{{% notice info %}} - Page being translated from -English. Do you speak Japanese? Help us to translate -it by sending us pull requests! -{{% /notice %}} - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} - ## 要素から要素の検索 親要素のコンテキスト内で子要素を見つけるために使用します。 diff --git a/docs_source_files/content/webdriver/web_element.ko.md b/docs_source_files/content/webdriver/web_element.ko.md index dda43856eed8..b7729491a04a 100644 --- a/docs_source_files/content/webdriver/web_element.ko.md +++ b/docs_source_files/content/webdriver/web_element.ko.md @@ -226,173 +226,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -{{% notice info %}} - Page being translated from -English. Do you speak Korean? Help us to translate -it by sending us pull requests! -{{% /notice %}} - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} ## Find Element From Element diff --git a/docs_source_files/content/webdriver/web_element.nl.md b/docs_source_files/content/webdriver/web_element.nl.md index 10ab1d830957..514df7ee7b57 100644 --- a/docs_source_files/content/webdriver/web_element.nl.md +++ b/docs_source_files/content/webdriver/web_element.nl.md @@ -226,167 +226,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} ## Find Element From Element diff --git a/docs_source_files/content/webdriver/web_element.pt-br.md b/docs_source_files/content/webdriver/web_element.pt-br.md index 9fbbaa033bbc..6f0164ec2929 100644 --- a/docs_source_files/content/webdriver/web_element.pt-br.md +++ b/docs_source_files/content/webdriver/web_element.pt-br.md @@ -224,173 +224,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -{{% notice info %}} - Page being translated from -English. Do you speak Portuguese? Help us to translate -it by sending us pull requests! -{{% /notice %}} - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} ## Encontrar Elemento a partir do Elemento diff --git a/docs_source_files/content/webdriver/web_element.zh-cn.md b/docs_source_files/content/webdriver/web_element.zh-cn.md index 05a6bf3b81a8..77307f40f15f 100644 --- a/docs_source_files/content/webdriver/web_element.zh-cn.md +++ b/docs_source_files/content/webdriver/web_element.zh-cn.md @@ -222,173 +222,6 @@ fun main() { {{< / code-panel >}} {{< / code-tab >}} -### Find Elements using relative locators - -{{% notice info %}} - Page being translated from -English. Do you speak Chinese (simplified)? Help us to translate -it by sending us pull requests! -{{% /notice %}} - -Note: This feature is available in Selenium 4 or later - -These feature allows you to find Elements in the DOM based off the position -of another element using relative locations like `above`, `below`, `toLeftOf`, -and `toRightOf`. The Elements should be returned in proximity order. - - -{{< code-tab >}} - {{< code-panel language="java" >}} - import org.openqa.selenium.By; - import org.openqa.selenium.WebDriver; - import org.openqa.selenium.WebElement; - import org.openqa.selenium.chrome.ChromeDriver; - import java.util.List; - - import static org.openqa.selenium.support.locators.RelativeLocator.withTagName; - - public class findElementsUsingRelativeLocators { - public static void main(String[] args) { - WebDriver driver = new ChromeDriver(); - try { - driver.get("https://example.com"); - - // Get element with tag name 'div' - WebElement fromElement = driver.findElement(By.tagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement` - List elements = driver.findElements( - withTagName("p").above(fromElement) - ); - for (WebElement e : elements) { - System.out.println(e.getText()); - } - } finally { - driver.quit(); - } - } - } - {{< / code-panel >}} - {{< code-panel language="python" >}} -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.support.relative_locator import with_tag_name - -driver = webdriver.Chrome() -driver.get("https://www.example.com") - -# Get element with tag name 'div' -form_element = driver.find_element(By.TAG_NAME, 'div') - -# Get all the elements available with tag name 'p' above -# WebElement `fromElement`' -elements = elements = driver.find_elements(with_tag_name("p").above(form_element)) -for e in elements: - print(e.text) - {{< / code-panel >}} - {{< code-panel language="csharp" >}} -using OpenQA.Selenium; -using OpenQA.Selenium.Chrome; -using System.Collections.Generic; - -namespace FindElementsUsingRelativeLocators { - class FindElementsUsingRelativeLocators { - public static void Main(string[] args) { - IWebDriver driver = new ChromeDriver(); - try { - driver.Navigate().GoToUrl("https://example.com"); - - // Get element with tag name 'div' - IWebElement fromElement = driver.FindElement(By.TagName("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - ReadOnlyCollection elements = driver.FindElements( - RelativeBy.WithTagName("p").Above(fromElement) - ); - foreach(IWebElement e in elements) { - System.Console.WriteLine(e.Text); - } - } finally { - driver.Quit(); - } - } - } -} - {{< / code-panel >}} - {{< code-panel language="ruby" >}} - require 'selenium-webdriver' - driver = Selenium::WebDriver.for :chrome - begin - # Navigate to URL - driver.get 'https://www.example.com' - - # Get element with tag name 'div' - from_element = driver.find_element(:tag_name,'div') - - # Get all the elements available with tag name 'p' above - # WebElement `fromElement`' - elements = element.find_elements( - relative: {tag_name: 'p', above: from_element} - ) - - elements.each { |e| - puts e.text - } - ensure - driver.quit - end - {{< / code-panel >}} - {{< code-panel language="javascript" >}} - const {Builder, By} = require('selenium-webdriver'); - - (async function example() { - let driver = new Builder() - .forBrowser('chrome') - .build(); - - await driver.get('https://www.example.com'); - - // Get element with tag name 'div' - let fromElement = await driver.findElement(By.css("div")); - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement' - let elements = await driver.findElements( - withTagName('p').above(fromElement) - ); - for(let e of elements) { - console.log(await e.getText()); - } - })(); - {{< / code-panel >}} - {{< code-panel language="kotlin" >}} - import org.openqa.selenium.By - import org.openqa.selenium.chrome.ChromeDriver - - fun main() { - val driver = ChromeDriver() - try { - driver.get("https://example.com") - - // Get element with tag name 'div' - val fromElement = driver.findElement(By.tagName("div")) - - // Get all the elements available with tag name 'p' above - // WebElement `fromElement'' - val elements = driver.findElements( - withTagName("p").above(fromElement) - ) - for (e in elements) { - println(e.text) - } - } finally { - driver.quit() - } - } - {{< / code-panel >}} -{{< / code-tab >}} ## Find Element From Element