Interact with web elements with the sendKey (), click () …. events in Selenium

Tram Ho

Each website has many different elements such as textbox, button, radiobutton, selectbox, passwordbox, … to easily interact with the above elements, we can use methods supported by Selenium Web. Driver In the Java language, Selenium provides an API to find elements and perform actions on them, such as inputting textboxes, clicking buttons, etc.In addition, there are many methods from the available libraries that Selenium has. will support each of the different element types.

First, in order to interact with the web element, first of all, we need to find that element, and pass it a variable name, and start interacting with the web element through that variable. . You can refer to the article:
How to use findElement / s in Selenium?

I. Use basic commands

1. The sendKey () method

There are 2 different types of inputboxes:

  • Textbox: Will display the content of the screen textbox after transmitting.
  • Passwordbox: Will display the content as special characters like “******” or “……………….” to hide the required information. as secure as the password.

Mode of transmission:

  • Step 1: Find the locator of the email textbox
  • Step 2: Find the locator of the password textbox
  • Step 3: Transfer the content you want to input into the email textbox with the sendKey () method.
  • Step 4: Pass the content you want to input into the password textbox using the sendKey () method

2. The method clear ()

Clear () is a method that you can easily delete the contents of any field.

3. Click method ()

With buttons, links, …. on the screen, you can easily click on it through the command [namebutton] .click () For example:

  • Step 1: Find the locator of the Sign in button
  • Step 2: Pass the click event via the following code

4. Method of submit ()

When operating on a form, in addition to using the click () command on the submit button to submit the form, Selenium also supports a method of submit (). You can do the following: When the submit () command is used, WebDriver will look up the DOM to find the web element and activate its submit function.

II. Problems arise and solutions

If, when using the statements, you get a NoSuchElementException () error, that means the original FindElement statement is returning a null value. To handle this problem, the fastest ways you can do it are:

  • Is the findElement position correct? (based on inspection)
  • Is the value you use in code different from the value of the current Firepath element?
  • Some properties are programmed dynamically, so it will cause trouble for us to search for their locations, you can use by.xpath () or by.cssSector () to ensure more accuracy.
  • Retry a few runs, maybe an error is caused by some wait behavior of findElement ()

III. summary

ElementThe statementDetail
InputBoxsendKeys ()To transfer content to the textbox
InputBoxclear ()To delete all content in the textbox
Link, Buttonclick ()To click on a certain link or button, it will wait for the page to finish loading after clicking before proceeding to the next statement
SubmitButton / Formsubmit ()To submit the form’s content
  • WebDriver allows to select more than 1 option in cases interacting with selectbox, dropdownbox …. (these types allow to select multiple options)
  • You can use the submit () statement at any button in the form. WebDriver will automatically activate the submit function for that button’s form.

Reference source: https://www.guru99.com/accessing-forms-in-webdriver.html

Share the news now

Source : Viblo