Java+selenium element positioning and element manipulation

Source: Internet
Author: User
Tags gettext tag name

1. Element positioning

ID anchor element:

Findelement (By.id (""));

To locate an element by its name:

Findelement (By.name (""));

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

Findelement (By.xpath (""));

To locate an element by its tag name:

Findelement (By.tagname (""));

Elements are positioned through the link name of the element:

Findelement (By.linktext (""));

Elements are positioned through the class name of the element:

Findelement (By.classname (""));

elements are positioned via CSS elements:

Findelement (By.cssselector (""));

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

Findelement (By.partiallinktext (""));

2. Element manipulation

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

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

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

3, page operation

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 ();

3, Input Box

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:

4. Drop-down selection box

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 ();

5. Radio Box

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

6. Multi-check box

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

Checkbox.click ();

Checkbox.clear ();

Checkbox.isselected ();

Checkbox.isenabled ();

6. Popup dialog box

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

Alert.accept (); Are you sure

Alert.dismiss (); Cancel

Alert.gettext (); Get text

7. Forms

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

Approve.click ();

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

8. 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);

9.window and IFRAME switching

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

10. Call JS

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

Javascriptexecutor js = (javascriptexecutor) driver;

Js.executescript ("JS script");

11. 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

Java+selenium element positioning and element manipulation

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.