使用 Selenium 最基本的特点之一是获取可用于操作的元素引用。
Selenium 提供了许多内置的 定位策略,用于唯一标识元素。
在更复杂的场景中,可以用多种方式使用这些定位器。为了本篇文档的目的,
我们来考虑下面的 HTML 片段:
<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>
第一个匹配的元素
许多定位器会匹配页面上的多个元素。
单个的 find element 方法会返回在给定上下文中找到的第一个元素的引用。
在整个 DOM 中查找
当在 driver 实例上调用 find element 方法时,
它会返回 DOM 中与所提供定位器匹配的第一个元素的引用。
该引用可以被保存并用于后续的元素操作。
在上面的示例 HTML 中,有两个 class 名称为 “tomatoes” 的元素,
因此此方法会返回位于 “vegetables” 列表中的那个元素。
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')endit'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}endit'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')endit'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}endit'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()
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')endit'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}endit'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')endit'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}endit'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')endit'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}endit'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')endit'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}endit'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()}}
获取活动元素
用于跟踪或查找当前浏览上下文中具有焦点的 DOM 元素。
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')endit'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}endit'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()}}