定位器策略 – Selenium Python
Selenium Python中的定位器策略是用于从页面中定位元素并对其执行操作的方法。 Selenium 的Python模块是为使用Python执行自动化测试而构建的。 Selenium Python绑定提供了一个简单的 API 来使用Selenium WebDriver 编写功能/验收测试。在安装了selenium并签出 – 使用 get 方法导航链接之后,您可能想更多地使用Selenium Python。在使用selenium (如 geeksforgeeks)打开页面后,您可能希望自动单击某些按钮或自动填写表格或任何此类自动化任务。本文围绕两种策略展开——定位单个元素和定位多个元素。
定位单个第一个元素的定位器策略
Selenium Python遵循不同的元素定位策略。可以通过 8 种不同的方式定位元素。以下是Python中Selenium的定位策略列表——
Locators | Description |
---|---|
find_element_by_id | The first element with the id attribute value matching the location will be returned. |
find_element_by_name | The first element with the name attribute value matching the location will be returned. |
find_element_by_xpath | The first element with the xpath syntax matching the location will be returned. |
find_element_by_link_text | The first element with the link text value matching the location will be returned. |
find_element_by_partial_link_text | The first element with the partial link text value matching the location will be returned. |
find_element_by_tag_name | The first element with the given tag name will be returned. |
find_element_by_class_name | the first element with the matching class attribute name will be returned. |
find_element_by_css_selector | The first element with the matching CSS selector will be returned. |
定位多个元素的定位器策略
Selenium Python遵循不同的元素定位策略。可以通过 8 种不同的方式定位多个元素。以下是Python中Selenium的定位策略列表——
Locators | Description |
---|---|
find_elements_by_name | All elements with name attribute value matching the location will be returned. |
find_elements_by_xpath | All elements with xpath syntax matching the location will be returned. |
find_elements_by_link_text | All elements with link text value matching the location will be returned. |
find_elements_by_partial_link_text | All elements with partial link text value matching the location will be returned. |
find_elements_by_tag_name | All elements with given tag name will be returned. |
find_elements_by_class_name | All elements with matching class attribute name will be returned. |
find_elements_by_css_selector | All elements with matching CSS selector will be returned. |