Selenium-java (first article)

Source: Internet
Author: User
Tags gettext tag name

Positioning and manipulation of selenium elements

1. Positioning of elements

Selenium

Automation, the most basic thing we need to do is to find the element on the page and pass the script

Sequence to manipulate this element to simulate manual operation. We have a variety of positioning elements to choose from:

Through the element's

ID anchor element:

Findelement (By.id (Element));

To locate an element by its name:

Findelement (By.name (Element));

Position elements by their location in the HTML of the element:

Findelement (By.xpath (Element));

To locate an element by its tag name:

findelement (by. TagName (Element));

Elements are positioned through the link name of the element:

Findelement (By.linktext (Element));

Elements are positioned through the class name of the element:

Findelement (By.classname (Element));

elements are positioned via CSS elements:

Findelement (By.cssselector (Element));

Elements are positioned by part of the link name of the element:

Findelement (By.partiallinktext (Element));

2. Operation of elements

Different elements we do different things, button clicks, input box character input, or elements inside

The different elements of our operation differ in the way they are obtained.

Click on elements with ID element:

Driver.findelement (By.id (Element)). Click ();

Send character operations on elements with ID element:

Driver.findelement (By.id (Element)). SendKeys ("XXXXXX");

Here only the most and simplest of the two elements of the operation,

And then we can get the elements first, then we'll do it alone.

Webelement Text1 = driver.findelement (by.name ("password"));
Text1.sendkeys ("123456");

3. Page operation

Analog mouse click: Driver.click ();

Open Web page: Driver.get ("https://www.baidu.com");

Close webpage: driver.close ();

Enter the content in the input box: Text.sendkeys ("");

Empty the contents of the Input box: Text.clear ();

Get the contents of the Input box: Text.gettext ();

Select the element in the drop-down box:

Select select = New Select (Wd.findelement (By.id ("select"));

Switch to a frame:

Driver.switchto (). FRAME ("");

Switch from one frame to another frame:

Driver.switchto (). FRAME ("");

Switch to a window:

Driver.switchto (). Window ("Windowname");

Returns the parent IFRAME: (this statement is usually written before the jump frame)

Driver.switchto (). Defaultcontent ();

Refresh page: Driver.navigate (). Refresh ();

Page forward back:

Driver.navigate (). Forward ();

Driver.navigate (). back ();

Hover Mouse:

1.5.1 Input box (text field or textarea)

webelement element = Driver.findelement (By.id ("Passwd-id"));

    • Element.sendkeys ("test");//Enter the contents in the Input box:
    • Element.clear (); Empty the input box
    • Element.gettext (); Get the text content of the input box:
1.5.2 drop-down selection box (select)

Select select = New Select (Driver.findelement (By.id ("select"));

    • Select.selectbyvisibletext ("A");
    • Select.selectbyvalue ("1");
    • Select.deselectall ();
    • Select.deselectbyvalue ("1");
    • Select.deselectbyvisibletext ("A");
    • Select.getallselectedoptions ();
    • Select.getfirstselectedoption ();
1.5.3 single option (Radio Button)

Webelement radio=driver.findelement (by.id ("Bookmode"));

    • Radio.click (); Select a single option
    • Radio.clear (); Clear a single option
    • Radio.isselected (); Determine if a single option has been selected
1.5.4 Multiple options (checkbox)

webelement checkbox = Driver.findelement (By.id ("MyCheckBox."));

    • Checkbox.click ();
    • Checkbox.clear ();
    • Checkbox.isselected ();
    • Checkbox.isenabled ();
1.5.5 buttons (button)

Webelement btn= driver.findelement (by.id ("Save"));

    • Btn.click (); Click the button
    • Btn.isenabled (); Determines whether the button is enable
1.5.7 Popup dialog box (popup dialogs)

Alert alert = Driver.switchto (). alert ();

    • Alert.accept (); Are you sure
    • Alert.dismiss (); Cancel
    • Alert.gettext (); Get text
1.5.8 form (form)

The actions of elements in a form, like other element operations, can be submitted to the form after the element operation is complete:

webelement approve = driver.findelement (By.id ("approve"));

Approve.click ();

Or

Approve.submit ();//only suitable for form submission

1.5.9 Uploading Files

To upload an element of a file operation:

Webelement adfileupload =driver.findelement (by.id ("Wap-upload"));

String FilePath = "C:\test\\uploadfile\\media_ads\\test.jpg";

Adfileupload.sendkeys (FilePath);

1.6 Switching between Windows and frames
    • Driver.switchto (). Defaultcontent (); Return to the top-most frame/iframe
    • Driver.switchto (). FRAME ("Leftframe"); Switch to a frame:
    • Driver.switchto (). Window ("Windowname"); Switch to a window
1.7 Calling Java Script

Web driver calls to Java script are implemented through Javascriptexecutor, for example:

Javascriptexecutor js = (javascriptexecutor) driver;

Js.executescript ("JS script");

1.8 Timeout settings

Webdriver Driver = new Firefoxdriver ();

    • Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Time-out when element is recognized
    • Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Time-out at page load
    • Driver.manage (). Timeouts (). Setscripttimeout (Timeunit.seconds); Time-out for asynchronous scripts

Selenium-java (first article)

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.