Java Selenium Common Web UI element operations and API usage _java

Source: Internet
Author: User
Tags pear xpath

This article describes how we use selenium to manipulate various page elements

Reading Table of Contents

    1. Links (Link)
    2. Input Box textbox
    3. Buttons (Button)
    4. Drop-down selection box (select)
    5. Radio buttons (Radio button)
    6. Multi-marquee check box

Links (Link)

  <div>
  <p> links link</p>
  <a href= "Www.cnblogs.com/tankxiao" > Small tank </a>
 </ Div>

Linked actions

 Find link element
  webelement link1 = driver.findelement (By.linktext ("Small Tank"));
  Webelement link11 = driver.findelement (By.partiallinktext ("Tank"));
  
  Click on the link
  Link1.click ();

Input Box textbox

 <div>
  <p> input box testbox</p>
  <input type= "text" id= "Usernameid" value= "username"/>
 </div>

The action of the input box

  Find element
  webelement = driver.findelement (By.id ("Usernameid"));
  
  Enter the content in the input box
  element.sendkeys ("test111111");
  
  Empty the input box
  element.clear ();
  
  Gets the content
  element.getattribute ("value") of the input box;

Buttons (Button)

 <div>
  <p> button button</p>
  <input type= "button" value= "Add" id= "Proadditem_0"/>
 

Find the button element

  Find the button element
  String xpath= "//input[@value = ' add ']";
  Webelement AddButton = driver.findelement (By.xpath (XPath));

  Click on the button
  Addbutton.click ();

  Determine if the button is enable
  addbutton.isenabled ();

Drop-down selection box (select)

<div>
  <p> drop-down selection box select</p>
  <select id= "Proadditem_kind" name= "kind" >
   < Option value= "1" > Computer hardware </option>
   <option value= "2" > Real estate </option>
   <option value= "18" > Type aa</option>
   <option value= "A" > Kind bb</option>
   <option value= "A" > Kind bb</ option>
   <option value= "> Type cc</option>
  </select>
 </div>

Action of the Drop-down selection box

 Find the element
  Select select = New Select (Driver.findelement (by.id ("Proadditem_kind")));

  Select the corresponding selection, index
  Select.selectbyindex (2) starting from 0;
  Select.selectbyvalue ("a");
  Select.selectbyvisibletext ("kind AA");

  Get all the options
  list<webelement> = Select.getoptions ();
  for (Webelement webelement:options) {
   System.out.println (Webelement.gettext ()); 
  }

Radio buttons (Radio button)

 <div>
  <p> single option Radio button</p>
  <input type= "Radio" value= "Apple" name= "fruit>"/> Apple
  <input type= "Radio" value= "Pear" name= "fruit>"/>pear "<input" type= "Radio" value= "Banana"
  Name= "fruit>"/>banana
  <input type= "Radio" value= "Orange" name= "fruit>"/>orange
 </div >

Action for a single OPTION element

 Locate the Radio box element
  String xpath= "//input[@type = ' Radio '] [@value = ' Apple ']";
  Webelement Apple = driver.findelement (By.xpath (XPath));

  Select a single marquee
  Apple.click ();

  Determines whether a single marquee has been selected
  Boolean isappleselect = apple.isselected ();

  Gets the element attribute
  Apple.getattribute ("value");

Multi-marquee check box

 <div>
  <p> Multi-option checkbox</p>
  <input type= "checkbox" value= "Apple" name= "fruit>"/> Apple
  <input type= "checkbox" value= "Pear" name= "fruit>"/>pear <input type=
  "checkbox" Value= " Banana "Name=" fruit> "/>banana
  <input type=" checkbox "value=" Orange "name=" fruit> "/>orange
 </div>

The operation of the multiple-selection box is the same as the one of the marquee, which is no longer spoken here.

The above is the Java Selenium Common Web UI element operation data collation, follow-up continue to add, thank you for your support for this site!

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.