Locating the elements based on the provided locator values.
One of the most fundamental aspects of using Selenium is obtaining element references to work with.
Selenium offers a number of built-in locator strategies to uniquely identify an element.
There are many ways to use the locators in very advanced scenarios. For the purposes of this documentation,
use the Selenium locator test page.
First matching element
Many locators will match multiple elements on the page. The singular find element method will return a reference to the
first element found within a given context.
Evaluating entire DOM
When the find element method is called on the driver instance, it
returns a reference to the first element in the DOM that matches with the provided locator.
This value can be stored and used for future element actions. On the Selenium locator test page, there are
two elements with the class name information, so this method returns the first text input.
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Finders'dolet(:driver){start_session}context'without executing finders',skip:'these are just examples, not actual tests'doit'finds the first matching element'dodriver.find_element(class:'tomatoes')endit'uses a subset of the dom to find an element'dofruits=driver.find_element(id:'fruits')fruits.find_element(class:'tomatoes')endit'uses an optimized locator'dodriver.find_element(css:'#fruits .tomatoes')endit'finds all matching elements'dodriver.find_elements(tag_name:'li')end# rubocop:disable RSpec/Outputit'gets an element from a collection'doelements=driver.find_elements(:tag_name,'p')elements.each{|e|putse.text}endit'finds element from element'doelement=driver.find_element(:tag_name,'div')elements=element.find_elements(:tag_name,'p')elements.each{|e|putse.text}end# rubocop:enable RSpec/Outputit'find active element'dodriver.find_element(css:'[name="q"]').send_keys('webElement')driver.switch_to.active_element.attribute('title')endendend
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Finders'dolet(:driver){start_session}context'without executing finders',skip:'these are just examples, not actual tests'doit'finds the first matching element'dodriver.find_element(class:'tomatoes')endit'uses a subset of the dom to find an element'dofruits=driver.find_element(id:'fruits')fruits.find_element(class:'tomatoes')endit'uses an optimized locator'dodriver.find_element(css:'#fruits .tomatoes')endit'finds all matching elements'dodriver.find_elements(tag_name:'li')end# rubocop:disable RSpec/Outputit'gets an element from a collection'doelements=driver.find_elements(:tag_name,'p')elements.each{|e|putse.text}endit'finds element from element'doelement=driver.find_element(:tag_name,'div')elements=element.find_elements(:tag_name,'p')elements.each{|e|putse.text}end# rubocop:enable RSpec/Outputit'find active element'dodriver.find_element(css:'[name="q"]').send_keys('webElement')driver.switch_to.active_element.attribute('title')endendend
Java and C# WebDriver, WebElement and ShadowRoot classes all implement a SearchContext interface, which is
considered a role-based interface. Role-based interfaces allow you to determine whether a particular
driver implementation supports a given feature. These interfaces are clearly defined and try
to adhere to having only a single role of responsibility.
Evaluating the Shadow DOM
The Shadow DOM is an encapsulated DOM tree hidden inside an element.
With the release of v96 in Chromium Browsers, Selenium can now allow you to access this tree with
easy-to-use shadow root methods. NOTE: These methods require Selenium 4.0 or greater.
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Finders'dolet(:driver){start_session}context'without executing finders',skip:'these are just examples, not actual tests'doit'finds the first matching element'dodriver.find_element(class:'tomatoes')endit'uses a subset of the dom to find an element'dofruits=driver.find_element(id:'fruits')fruits.find_element(class:'tomatoes')endit'uses an optimized locator'dodriver.find_element(css:'#fruits .tomatoes')endit'finds all matching elements'dodriver.find_elements(tag_name:'li')end# rubocop:disable RSpec/Outputit'gets an element from a collection'doelements=driver.find_elements(:tag_name,'p')elements.each{|e|putse.text}endit'finds element from element'doelement=driver.find_element(:tag_name,'div')elements=element.find_elements(:tag_name,'p')elements.each{|e|putse.text}end# rubocop:enable RSpec/Outputit'find active element'dodriver.find_element(css:'[name="q"]').send_keys('webElement')driver.switch_to.active_element.attribute('title')endendend
There are several use cases for needing to get references to all elements that match a locator, rather
than just the first one. The plural find elements methods return a collection of element references.
If there are no matches, an empty list is returned. In this case,
references to all input elements will be returned in a collection.
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Finders'dolet(:driver){start_session}context'without executing finders',skip:'these are just examples, not actual tests'doit'finds the first matching element'dodriver.find_element(class:'tomatoes')endit'uses a subset of the dom to find an element'dofruits=driver.find_element(id:'fruits')fruits.find_element(class:'tomatoes')endit'uses an optimized locator'dodriver.find_element(css:'#fruits .tomatoes')endit'finds all matching elements'dodriver.find_elements(tag_name:'li')end# rubocop:disable RSpec/Outputit'gets an element from a collection'doelements=driver.find_elements(:tag_name,'p')elements.each{|e|putse.text}endit'finds element from element'doelement=driver.find_element(:tag_name,'div')elements=element.find_elements(:tag_name,'p')elements.each{|e|putse.text}end# rubocop:enable RSpec/Outputit'find active element'dodriver.find_element(css:'[name="q"]').send_keys('webElement')driver.switch_to.active_element.attribute('title')endendend
Often you get a collection of elements but want to work with a specific element, which means you
need to iterate over the collection and identify the one you want.
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
usingOpenQA.Selenium;usingOpenQA.Selenium.Firefox;usingSystem.Collections.Generic;namespaceFindElementsExample{classFindElementsExample{publicstaticvoidMain(string[]args){IWebDriverdriver=newFirefoxDriver();try{// Navigate to Urldriver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/locators_tests/locators.html");// Get all the elements available with tag name 'p'IList<IWebElement>elements=driver.FindElements(By.TagName("p"));foreach(IWebElementeinelements){System.Console.WriteLine(e.Text);}}finally{driver.Quit();}}}}
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Finders'dolet(:driver){start_session}context'without executing finders',skip:'these are just examples, not actual tests'doit'finds the first matching element'dodriver.find_element(class:'tomatoes')endit'uses a subset of the dom to find an element'dofruits=driver.find_element(id:'fruits')fruits.find_element(class:'tomatoes')endit'uses an optimized locator'dodriver.find_element(css:'#fruits .tomatoes')endit'finds all matching elements'dodriver.find_elements(tag_name:'li')end# rubocop:disable RSpec/Outputit'gets an element from a collection'doelements=driver.find_elements(:tag_name,'p')elements.each{|e|putse.text}endit'finds element from element'doelement=driver.find_element(:tag_name,'div')elements=element.find_elements(:tag_name,'p')elements.each{|e|putse.text}end# rubocop:enable RSpec/Outputit'find active element'dodriver.find_element(css:'[name="q"]').send_keys('webElement')driver.switch_to.active_element.attribute('title')endendend
const{Builder,By}=require('selenium-webdriver');(asyncfunctionexample(){letdriver=awaitnewBuilder().forBrowser('firefox').build();try{// Navigate to Url
awaitdriver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');// Get all the elements available with tag 'p'
letelements=awaitdriver.findElements(By.css('p'));for(leteofelements){console.log(awaite.getText());}}finally{awaitdriver.quit();}})();
importorg.openqa.selenium.Byimportorg.openqa.selenium.firefox.FirefoxDriverfunmain(){valdriver=FirefoxDriver()try{driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html")// Get all the elements available with tag name 'p'
valelements=driver.findElements(By.tagName("p"))for(elementinelements){println("Paragraph text:"+element.text)}}finally{driver.quit()}}
Find Elements From Element
It is used to find the list of matching child WebElements within the context of parent element.
To achieve this, the parent WebElement is chained with ‘findElements’ to access child elements
importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importjava.util.List;publicclassfindElementsFromElement{publicstaticvoidmain(String[]args){WebDriverdriver=newChromeDriver();try{driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html");// Get the form elementWebElementelement=driver.findElement(By.tagName("form"));// Get all input elements in the formList<WebElement>elements=element.findElements(By.tagName("input"));for(WebElemente:elements){System.out.println(e.getAttribute("value"));}}finally{driver.quit();}}}
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
usingOpenQA.Selenium;usingOpenQA.Selenium.Chrome;usingSystem.Collections.Generic;namespaceFindElementsFromElement{classFindElementsFromElement{publicstaticvoidMain(string[]args){IWebDriverdriver=newChromeDriver();try{driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/locators_tests/locators.html");// Get the form elementIWebElementelement=driver.FindElement(By.TagName("form"));// Get all input elements in the formIList<IWebElement>elements=element.FindElements(By.TagName("input"));foreach(IWebElementeinelements){System.Console.WriteLine(e.GetAttribute("value"));}}finally{driver.Quit();}}}}
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Finders'dolet(:driver){start_session}context'without executing finders',skip:'these are just examples, not actual tests'doit'finds the first matching element'dodriver.find_element(class:'tomatoes')endit'uses a subset of the dom to find an element'dofruits=driver.find_element(id:'fruits')fruits.find_element(class:'tomatoes')endit'uses an optimized locator'dodriver.find_element(css:'#fruits .tomatoes')endit'finds all matching elements'dodriver.find_elements(tag_name:'li')end# rubocop:disable RSpec/Outputit'gets an element from a collection'doelements=driver.find_elements(:tag_name,'p')elements.each{|e|putse.text}endit'finds element from element'doelement=driver.find_element(:tag_name,'div')elements=element.find_elements(:tag_name,'p')elements.each{|e|putse.text}end# rubocop:enable RSpec/Outputit'find active element'dodriver.find_element(css:'[name="q"]').send_keys('webElement')driver.switch_to.active_element.attribute('title')endendend
const{Builder,By}=require('selenium-webdriver');(asyncfunctionexample(){letdriver=newBuilder().forBrowser('chrome').build();awaitdriver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');// Get the form element
letelement=driver.findElement(By.css("form"));// Get all input elements in the form
letelements=awaitelement.findElements(By.css("input"));for(leteofelements){console.log(awaite.getAttribute("value"));}})();
importorg.openqa.selenium.Byimportorg.openqa.selenium.chrome.ChromeDriverfunmain(){valdriver=ChromeDriver()try{driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html")// Get the form element
valelement=driver.findElement(By.tagName("form"))// Get all input elements in the form
valelements=element.findElements(By.tagName("input"))for(einelements){println(e.getAttribute("value"))}}finally{driver.quit()}}
Get Active Element
It is used to track (or) find DOM element which has the focus in the current browsing context.
importorg.openqa.selenium.*;importorg.openqa.selenium.chrome.ChromeDriver;publicclassactiveElementTest{publicstaticvoidmain(String[]args){WebDriverdriver=newChromeDriver();try{driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html");driver.findElement(By.cssSelector("#fname")).sendKeys("webElement");// Get attribute of current active elementStringattr=driver.switchTo().activeElement().getAttribute("name");System.out.println(attr);}finally{driver.quit();}}}
importpytestfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy# The tests below marked as skipped mirror the HTML snippet shown at the top of the# "Finding web elements" documentation and are illustrative only, matching how the# same examples are shown for the other language bindings:## <ol id="vegetables"># <li class="potatoes">…# <li class="onions">…# <li class="tomatoes"><span>Tomato is a Vegetable</span>…# </ol># <ul id="fruits"># <li class="bananas">…# <li class="apples">…# <li class="tomatoes"><span>Tomato is a Fruit</span>…# </ul>@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_basic_finders(driver):vegetable=driver.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_subset_of_dom(driver):fruits=driver.find_element(By.ID,'fruits')fruit=fruits.find_element(By.CLASS_NAME,'tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_optimized_locator(driver):fruit=driver.find_element(By.CSS_SELECTOR,'#fruits .tomatoes')@pytest.mark.skip(reason="illustrative example, not an executable test")deftest_all_matching_elements(driver):plants=driver.find_elements(By.TAG_NAME,'li')deftest_evaluating_shadow_dom():driver=webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.selenium.dev/selenium/web/shadowRootPage.html')shadow_host=driver.find_element(By.TAG_NAME,'custom-checkbox-element')shadow_root=shadow_host.shadow_rootassertshadow_rootshadow_content=shadow_root.find_element(By.CSS_SELECTOR,'input[type=checkbox]')assertshadow_host.is_displayed()assertshadow_content.is_displayed()driver.quit()deftest_get_element():driver=webdriver.Chrome()driver.get('https://www.example.com')elements=driver.find_elements(By.TAG_NAME,'p')forelementinelements:print(element.text)assertlen(elements)>0driver.quit()deftest_find_elements_from_element():driver=webdriver.Chrome()driver.get('https://www.example.com')element=driver.find_element(By.TAG_NAME,'div')elements=element.find_elements(By.TAG_NAME,'p')foreinelements:print(e.text)assertlen(elements)>0driver.quit()deftest_get_active_element():driver=webdriver.Chrome()driver.get('https://www.selenium.dev/selenium/web/web-form.html')driver.find_element(By.CSS_SELECTOR,'[name="my-text"]').send_keys('webElement')attr=driver.switch_to.active_element.get_attribute('name')assertattr=='my-text'driver.quit()
usingOpenQA.Selenium;usingOpenQA.Selenium.Chrome;namespaceActiveElement{classActiveElement{publicstaticvoidMain(string[]args){IWebDriverdriver=newChromeDriver();try{// Navigate to Urldriver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/locators_tests/locators.html");driver.FindElement(By.CssSelector("#fname")).SendKeys("webElement");// Get attribute of current active elementstringattr=driver.SwitchTo().ActiveElement().GetAttribute("name");System.Console.WriteLine(attr);}finally{driver.Quit();}}}}
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Element Finders'dolet(:driver){start_session}context'without executing finders',skip:'these are just examples, not actual tests'doit'finds the first matching element'dodriver.find_element(class:'tomatoes')endit'uses a subset of the dom to find an element'dofruits=driver.find_element(id:'fruits')fruits.find_element(class:'tomatoes')endit'uses an optimized locator'dodriver.find_element(css:'#fruits .tomatoes')endit'finds all matching elements'dodriver.find_elements(tag_name:'li')end# rubocop:disable RSpec/Outputit'gets an element from a collection'doelements=driver.find_elements(:tag_name,'p')elements.each{|e|putse.text}endit'finds element from element'doelement=driver.find_element(:tag_name,'div')elements=element.find_elements(:tag_name,'p')elements.each{|e|putse.text}end# rubocop:enable RSpec/Outputit'find active element'dodriver.find_element(css:'[name="q"]').send_keys('webElement')driver.switch_to.active_element.attribute('title')endendend
const{Builder,By}=require('selenium-webdriver');(asyncfunctionexample(){letdriver=awaitnewBuilder().forBrowser('chrome').build();awaitdriver.get('https://www.selenium.dev/selenium/web/locators_tests/locators.html');awaitdriver.findElement(By.css('#fname')).sendKeys("webElement");// Get attribute of current active element
letattr=awaitdriver.switchTo().activeElement().getAttribute("name");console.log(`${attr}`)})();
importorg.openqa.selenium.Byimportorg.openqa.selenium.chrome.ChromeDriverfunmain(){valdriver=ChromeDriver()try{driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html")driver.findElement(By.cssSelector("#fname")).sendKeys("webElement")// Get attribute of current active element
valattr=driver.switchTo().activeElement().getAttribute("name")print(attr)}finally{driver.quit()}}