Selenium Python中的元素方法
Selenium 的Python模块是为使用Python执行自动化测试而构建的。 Python中的Selenium与元素一起工作。元素可以是标签、属性或任何东西,它是class selenium.webdriver.remote.webelement.WebElement
的实例。使用selenium在屏幕上找到元素后,您可能希望单击它或查找子元素等Selenium提供了围绕Selenium的 WebElement 的方法。在本文中,我们讨论了可用于使用Selenium及其 WebElement 执行多项任务的各种方法。
如何在Selenium Python中的元素上使用方法?
要在 WebElement 上使用方法,首先我们需要将其定位到网页中。如何在Selenium Python中定位元素有多种方法。结帐 – 定位器策略 – Selenium Python。抓取元素后,您可以根据以下语法使用方法 -
句法 -
element.method_name
例子 -
要找到一个元素,需要使用其中一种定位策略,例如,
element = driver.find_element_by_id("passwd-id")
element = driver.find_element_by_name("passwd")
element = driver.find_element_by_xpath("//input[@id='passwd-id']")
此外,要查找多个元素,我们可以使用 -
elements = driver.find_elements_by_name("passwd")
现在可以使用任何方法——
element.method_name
Selenium Python中的元素方法
Element Methods | Description |
---|---|
is_selected() | is_selected method is used to check if element is selected or not. It returns a boolean value True or False. |
is_displayed() | is_displayed method is used to check if element it visible to user or not. It returns a boolean value True or False. |
is_enabled() | is_enabled method is used to check if element is enabled or not. It returns a boolean value True or False. |
get_property() | get_property method is used to get properties of an element, such as getting text_length property of anchor tag. |
get_attribute() | get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag. |
send_keys() | send_keys method is used to send text to any field, such as input field of a form or even to anchor tag paragraph, etc. |
click() | click method is used to click on any element, such as an anchor tag, a link, etc. |
clear() | clear method is used to clear text of any field, such as input field of a form or even to anchor tag paragraph, etc. |
screenshot() | screenshot method is used to save a screenshot of current element to a PNG file. |
submit() | submit method is used to submit a form after you have sent data to a form. |
value_of_css_property() | value_of_css_property method is used to get value of a css property for a element. |
location | location method is used to get location of element in renderable canvas. |
screenshot_as_png | screenshot_as_png method is used to gets the screenshot of the current element as binary data. |
parent | parent method is used to get internal reference to the WebDriver instance this element was found from. |
size | size method is used to get size of current element. |
tag_name | tag_name method is used to get name of tag you are referring to. |
text | text method is used to get text of current element. |
rect | rect method is used to get a dictionary with the size and location of the element. |
screenshot_as_base64 | screenshot_as_base64 method is used to gets the screenshot of the current element as a base64 encoded string. |
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。