WebElement Commands in Kotlin Selenium

WebElement commands in selenium will be applicable to almost all the DOM elements on the web page. Every single controls present on the page is called a WebElement in WebPage.

Each of WebElement is represented in selenium by WebElement interface. WebElement interface is used to interact with both visible and invisible elements present on a page. So you should make that the element you are trying to access on the web page.

SendKeys() function:

sendKeys() sets a value into an element (editable input fields and dropdowns fields). This method accepts CharSequence as a parameter and returns nothing. CharSequence is top most interface for text values.

// set value in  WebElement
driver.findElement(By.id("locator")).sendKeys("some value")

Clear() function:

clear() will remove the value from the editable input fields like Textbar and text area, clear() function accepts nothing as a parameter and returns nothing.

// Clear  WebElement
driver.findElement(By.id("id of element")).clear()

Click() function:

Click() function is used to click any element, Accepts nothing as a parameter and returns nothing(type nothing).

// Click WebElement

text property:

text property will fetch the visible inner Text of the element, not only brings target element text but also brings the text from the inner elements of the target element.

// Get text of Particular WebElement & Store into String

tagName property :

tagName property gets the tag name of the Web Element. The tag name is nothing but the html tag which is used to build this element on the UI.

 // fetches the tag name

getAttribute function():

getAttribute(String Name) function gets the value of the given attribute of the element. Common attributes are like class, id, name, src, href. You can fetch any attribute fro the webelement

    driver.findElement(By.id("locator")).getAttribute("any attribute")

getCssValue functions():

getCssvalue( ) function fetches CSS property value of the give element. CSS properties are used for decorating the web element, for Example, font-size, corlor, background-color, padding so on.

driver.findElement(By.id("locator")).getCssvalue("any css property")

Size property:

size property fetches the width and height of the target element, returns the Dimension object. This returns the size of the element on the page.

val ele = driver.findElement(By.id("locator"))
val dimensions = ele.size
println("Height :" + dimensions.height + "Width : " + dimensions.width)

location property:

location property returns the location of the element on the page. getLocation returns the Point object, from which we can get X and Y coordinates of specific element.

val elementLocation = driver.findElement(By.id("locator"))

val point = elementLocation.location

println("X cordinate : " + point.x + "Y cordinate: " + point.y)

Submit() function:

submit( ) function submits a form, you should use this function only on elements which are present inside the <form> tag in html


isDisplayed property:

isDisplayed property is used to check if an element is currently being displayed or not on the webpage, this methods returns true if the element is displayed otherwise false.

// Verify WebElement is Present or Not?

isEnabled property:

isEnabled property determines if the element currently is Enabled or not?, this methods returns true if the element is enabled otherwise false.

    // Verify WebElement is Enabled or Not

isSelected property:

isSelected property checks whether the element is selected or not. This property can be applied on checkboxes and radio button an dropdowns as well. isSelected() return true if the element is selected otherwise false.

// Verify WebElement is Selected or Not

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

You can also share knowledge by Adding a topic here

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions