<olid="vegetables"><liclass="potatoes">…
<liclass="onions">…
<liclass="tomatoes"><span>Tomato is a Vegetable</span>…
</ol><ulid="fruits"><liclass="bananas">…
<liclass="apples">…
<liclass="tomatoes"><span>Tomato is a Fruit</span>…
</ul>
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
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
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()
usingOpenQA.Selenium;usingOpenQA.Selenium.Firefox;usingSystem.Collections.Generic;namespaceFindElementsExample{classFindElementsExample{publicstaticvoidMain(string[]args){IWebDriverdriver=newFirefoxDriver();try{// Navigate to Urldriver.Navigate().GoToUrl("https://example.com");// 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.example.com');// 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://example.com")// 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()}}
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://example.com");// Get element with tag name 'div'WebElementelement=driver.findElement(By.tagName("div"));// Get all the elements available with tag name 'p'List<WebElement>elements=element.findElements(By.tagName("p"));for(WebElemente:elements){System.out.println(e.getText());}}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://example.com");// Get element with tag name 'div'IWebElementelement=driver.FindElement(By.TagName("div"));// Get all the elements available with tag name 'p'IList<IWebElement>elements=element.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=newBuilder().forBrowser('chrome').build();awaitdriver.get('https://www.example.com');// Get element with tag name 'div'
letelement=driver.findElement(By.css("div"));// Get all the elements available with tag name 'p'
letelements=awaitelement.findElements(By.css("p"));for(leteofelements){console.log(awaite.getText());}})();
importorg.openqa.selenium.Byimportorg.openqa.selenium.chrome.ChromeDriverfunmain(){valdriver=ChromeDriver()try{driver.get("https://example.com")// Get element with tag name 'div'
valelement=driver.findElement(By.tagName("div"))// Get all the elements available with tag name 'p'
valelements=element.findElements(By.tagName("p"))for(einelements){println(e.text)}}finally{driver.quit()}}
importorg.openqa.selenium.*;importorg.openqa.selenium.chrome.ChromeDriver;publicclassactiveElementTest{publicstaticvoidmain(String[]args){WebDriverdriver=newChromeDriver();try{driver.get("http://www.google.com");driver.findElement(By.cssSelector("[name='q']")).sendKeys("webElement");// Get attribute of current active elementStringattr=driver.switchTo().activeElement().getAttribute("title");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.google.com");driver.FindElement(By.CssSelector("[name='q']")).SendKeys("webElement");// Get attribute of current active elementstringattr=driver.SwitchTo().ActiveElement().GetAttribute("title");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.google.com');awaitdriver.findElement(By.css('[name="q"]')).sendKeys("webElement");// Get attribute of current active element
letattr=awaitdriver.switchTo().activeElement().getAttribute("title");console.log(`${attr}`)})();
importorg.openqa.selenium.Byimportorg.openqa.selenium.chrome.ChromeDriverfunmain(){valdriver=ChromeDriver()try{driver.get("https://www.google.com")driver.findElement(By.cssSelector("[name='q']")).sendKeys("webElement")// Get attribute of current active element
valattr=driver.switchTo().activeElement().getAttribute("title")print(attr)}finally{driver.quit()}}