Any HTML element on a webpage can be called WebElement. HTML elements such as input tags, buttons, checkboxes , radio buttons, links etc.
Below are some actions which we can perform on the webelement.
It performs click action on given WebElement.It doesn’t take any argument and it doesn’t returns anything
element = wait.until(ec.presence_of_element_located((By.ID, "user_input"))) element.click()
This method enters a text in an editbox.It takes a string as an argument and it doesn’t return anything.
element = wait.until(ec.presence_of_element_located((By.ID, "user_input"))) element.send_keys("Skill2Lead")
This method works on text elements such as editbox etc. It clears the existing text.It doesn’t take any argument and it doesn’t returns anything.
element = wait.until(ec.presence_of_element_located((By.ID, "user_input"))) element.send_keys("Skill2Lead") element.clear()
This method verifies whether WebElement is located in a webpage within a visibility of screen or not.It doesn’t take any argument and it returns boolean value as true or false.
element = wait.until(ec.presence_of_element_located((By.ID, "user_input"))) element_displayed = element.is_displayed() print("Check whether element is Displayed with id 'user_input' : ", element_displayed)
This method verifies whether require WebElement is enabled or not to perform actions such as entering a text,clicking on a button etc.It doesn’t take any argument and it returns boolean value as true or false.
element = wait.until(ec.presence_of_element_located((By.ID, "user_input"))) element_enabled = element.is_enabled() print("Check whether element is enabled with id 'user_input' : ", element_enabled)
This method verifies whether a WebElement is selected or not.This method mostly applies only on checkboxes ,Radio buttons and other select options.It doesn’t take any argument and it returns boolean value true if the element is checked or selected , otherwise it returns false.
Let's discuss the is_selected() method in ‘Radio and Checkboxes’ chapter.
This method works similarly like click() method, But this mainly works only on forms submit button.It doesn’t take any argument and it doesn’t returns anything.
element.submit()
WebElement_Actions.py
from selenium import webdriver import time from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as ec driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe") driver.get("http://www.dummypoint.com/seleniumtemplate.html") time.sleep(2) wait = WebDriverWait(driver, 25, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, NoSuchElementException]) element = wait.until(ec.presence_of_element_located((By.ID, "user_input"))) element_displayed = element.is_displayed() print("Check whether element is Displayed with id 'user_input' : ", element_displayed) element_enabled = element.is_enabled() print("Check whether element is enabled with id 'user_input' : ", element_enabled) element.click() element.send_keys("Skill2Lead") time.sleep(2) element.clear() time.sleep(2) element.send_keys("Skill2Lead_second time") time.sleep(2) element.submit() time.sleep(5) driver.quit()