Types of Interactions in Selenium Webdriver

Posted By : Kishor Kumar Singh | 29-Nov-2018

How many types of interactions in selenium webdriver and how can we  properly use these interactions in scripts

 

click()


Click() method is straight forward as it sounds, it clicks on the element.
Note though, WebDriver only clicks on elements that are visible, and there is some rather complicated logic to determine if an element is visible. There is a discussion on this very topic by David Burns one of the selenium contributors.

 

getText()


 getText() method instructs WebDriver to return the text contained within an element.

<div>
<p>I'm Kishor</p>
<span>I created this course</span>
</div>
WebDriver returns all the text between the opening tag and the closing tag. So when we took the HTML above and the following code

driver.findElement(By.tagName("p")).getText();
it should be returned as ‘I’m Kishor’. As that is all the text between <p> and </p>.
Now if we asked WebDriver to return the text contained in the <div>, we’d get the contents of the <p> tag and the <span>.
So we would get

I'm Kishor
I created this course

 

sendKeys()


Mainly usage for SendKeys() is to populate text fields. We have to instruct WebDriver to find an input and populate it with our required text.

driver.findElement(By.id("firstname")).sendKeys("richard");
The result of this in WebDriver looking for an element with the ID of ‘firstname’ and when it finds it, it will try and type ‘Kishor’ into it.

You can use SendKeys() to send actually key presses. For example, when our site does some behaviour after populating some text in a field and pressing enter, we can simulate that with keys.

driver.findElement(By.id("firstname")).sendKeys(Keys.ENTER);


getAttribute()


Its a good method, even more so in past recent years with the additional attributes introduced with HTML5.
This method facilitates you to ask WebDriver to return you the value of any attribute on an element.

The most common usage is probably ‘value’. On an input field, this method would return you the text within the input.
Other common advantages of this method are to read the placeholder text to validate a form, get the classes, ID, names of elements. Also as mentioned, to read data attributes which are becoming more common.

driver.findElement(By.id("firstname")).getAttribute("value");
This would find an element with the id ‘firstname’, and return the value in the attribute input. Most Important to note that getText() would not have the same behaviour as this. getText() would not return you the value of an input field. Also, if you search this, you will very rarely see ‘value’ as an attribute when inspecting some HTML, but trust me, it works.

 

Driver Interactions

 


We also have some more ways in which we can interact with the Driver, or in other terms, the browser itself.
Those are getTitle() and getCurrentUrl().

getTitle() - ThisWill return you the text in the tab.
getCurrentUrl() - This will returns you the URL in the address bar.

 

All above methods are widely used in the selenium and are good in performance.

 

Related Tags

About Author

Author Image
Kishor Kumar Singh

Kishor Kumar Singh is highly dedicated towards work and he is a great team player. He believes that “No amount of testing can prove a software right, a single test can prove a software wrong".

Request for Proposal

Name is required

Comment is required

Sending message..