WebDriver API: Simple Element Operation Method Introduction, webdriverapi

Source: Internet
Author: User

WebDriver API: Simple Element Operation Method Introduction, webdriverapi

After locating an element, You need to operate it, or click (button) or enter (input box );
The most common WebDriver methods are as follows:

(1) clear () the content in the text input box

The clear () method is used to clear the content in the text input box. For example, in the logon box, there are usually prompts such as "Account" and "password" by default, which are used to guide users to enter correct data. However, if you enter data directly to the input box, it may be spliced with the prompt information in the input box. For example, if the user entered "username", but the information is spliced with the prompt information, it is changed to "account username", resulting in incorrect input information. In this case, you can use the clear () method to clear the default prompt information in the input box.

(2) sendKeys (* value) simulate the keyboard to input content to the input box

The sendKeys () method simulates the keyboard input content into the input box. In the preceding example, enter the logon information in the username and password boxes. Of course, it is not only used here, but we can also use it to send keyboard buttons, or even use it to simulate file upload.

(3) click () click an element

The click () method can be used to click an element,PrerequisitesIs an object that can be clicked. The sendKeys () method and sendKeys method are the two most commonly used methods in Web page operations. In fact, the click () method is not only used to click a button, but also allows you to click any text/image link, check box, single button, and drop-down box that can be clicked.

(4) The submit () method is used to submit a form.

For example, you can use the submit () method to simulate the "Press ENTER" operation after entering a keyword in the search box. Sometimes the submit () method can be used with the click () method. The submit () method can also submit a button, but the submit () method is far less widely used than click;

(5) getSize () return element size (6) getText () Get element text (7) getAttribute (name) Get attribute value (8) isDisplayed () sets whether the element is visible to the user.

Run the above program and view the result: the getSize method is used to obtain the width and height of the Baidu input box. The getText method is used to obtain
Filing information. GetAttribute () is used to obtain the value of the type attribute entered by Baidu. IsDisplayed () is used to return whether an element is visible.
True is returned, otherwise False is returned.
Of course, the WebElement interface also provides other methods. For more information, see the official WeBDriverAPI documentation.

WebDriver driver = new ChromeDriver (); driver. get (" https://www.126.com "); // Get the 126 email address driver. findElement (. id ("idInput ")). clear (); // clear the input box content driver. findElement (. id ("idInput ")). sendKeys ("username"); // enter the driver content in the input box. findElement (. id ("pwdInput ")). clear (); driver. findElement (. id ("pwdInput ")). sendKeys ("password"); driver. findElement (. id ("loginBtn ")). click (); // click the login button driver. findElement (. id ("query ")). sendKeys ("submit"); driver. findElement (. id ("query ")). submit (); // submit the input content driver. quit (); synchronized public static void main (String [] args) {WebDriver driver = new ChromeDriver (); driver. get (" https://www.baidu.com/ "); WebElement size = driver. findElement (. id ("kw"); // obtain the size of the Baidu input box System. out. println (size. getSize (); WebElement text = driver. findElement (. id ("cp"); // return the ICP filing information System at the bottom of the Baidu page. out. println (text. getText (); WebElement ty = driver. findElement (. id ("kw"); // return the attribute value of an element, which can be id, name, type, or any other attribute of the element System. out. println (ty. getAttribute ("type"); WebElement display = driver. findElement (. id ("kw"); // whether the returned element result is visible. The returned result is True or False System. out. println (display. isDisplayed (); driver. quit ();}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.