📜  定位器策略 – Selenium Python

📅  最后修改于: 2022-05-13 01:55:40.059000             🧑  作者: Mango

定位器策略 – Selenium Python

Selenium Python中的定位器策略是用于从页面中定位元素并对其执行操作的方法。 Selenium 的Python模块是为使用Python执行自动化测试而构建的。 Selenium Python绑定提供了一个简单的 API 来使用Selenium WebDriver 编写功能/验收测试。在安装了selenium并签出 – 使用 get 方法导航链接之后,您可能想更多地使用Selenium Python。在使用selenium (如 geeksforgeeks)打开页面后,您可能希望自动单击某些按钮或自动填写表格或任何此类自动化任务。本文围绕两种策略展开——定位单个元素和定位多个元素。
定位器-策略-Selenium-Python

定位单个第一个元素的定位器策略

Selenium Python遵循不同的元素定位策略。可以通过 8 种不同的方式定位元素。以下是Python中Selenium的定位策略列表——

LocatorsDescription
find_element_by_idThe first element with the id attribute value matching the location will be returned.
find_element_by_nameThe first element with the name attribute value matching the location will be returned.
find_element_by_xpathThe first element with the xpath syntax matching the location will be returned.
find_element_by_link_textThe first element with the link text value matching the location will be returned.
find_element_by_partial_link_textThe first element with the partial link text value matching the location will be returned.
find_element_by_tag_nameThe first element with the given tag name will be returned.
find_element_by_class_namethe first element with the matching class attribute name will be returned.
find_element_by_css_selectorThe first element with the matching CSS selector will be returned.

定位多个元素的定位器策略

Selenium Python遵循不同的元素定位策略。可以通过 8 种不同的方式定位多个元素。以下是Python中Selenium的定位策略列表——

LocatorsDescription
find_elements_by_nameAll elements with name attribute value matching the location will be returned.
find_elements_by_xpathAll elements with xpath syntax matching the location will be returned.
find_elements_by_link_textAll elements with link text value matching the location will be returned.
find_elements_by_partial_link_textAll elements with partial link text value matching the location will be returned.
find_elements_by_tag_nameAll elements with given tag name will be returned.
find_elements_by_class_nameAll elements with matching class attribute name will be returned.
find_elements_by_css_selectorAll elements with matching CSS selector will be returned.