Selenium’s Python Module is built to perform automated testing with Python. Selenium in Python works with elements. An element can be a tag, property, or anything, it is an instance of
class selenium.webdriver.remote.webelement.WebElement. After you find an element on screen using selenium, you might want to click it or find sub-elements, etc. Selenium provides methods around this WebElement of Selenium. In this article, we have discussed various methods that one can use to perform multiple tasks with Selenium and its WebElement.
How to use a method on an element in Selenium Python?
To use a method on a WebElement, first of all we need to locate it into the webpage. There are various methods n how to locate an element in Selenium Python. Checkout – Locator Strategies – Selenium Python. After you have grabbed an element you can use a method according to following syntax –
To find an element one needs to use one of the locating strategies, For example,
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']")
Also, to find multiple elements, we can use –
elements = driver.find_elements_by_name("passwd")
Now one can use any method as –
Element Methods in Selenium Python
|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.|
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.