java + selenium-- Find positioning element

 

 

Package rjcs;
 Import org.openqa.selenium.firefox.FirefoxDriver;
 public  class qhjb 
{ 
        public  static  void main (String [] args) 
        
        { 
             System.setProperty ( "webdriver.firefox.bin", "C: \\ Program Files (the x86 ) \\ \\ firefox.exe the Mozilla Firefox ");     // set the installation path Firefox, preventing system can not find 
                
             FirefoxDriver Driver = new new FirefoxDriver ();         // initialize FireFox browser instance, and open the browser 
             
            the try 
            { 
                 Driver. .. the Manage () window () the maximize ();          // maximized window
                 
                 Thread.sleep ( 5000 );         

            
                 driver.get ( "https://www.baidu.com");                     // open a URL, a method 

                 // driver.findElementByLinkText ( "map") .click ();     // by open connection map text 
                 
                 Boolean . driver.findElementById = A ( "kW") isDisplayed ();       // if seen 
                 System.out.println ( "elements are visible:" + A); 
                 
                 Boolean B = driver.findElementById ( "kW" ) .isEnabled ();    // whether editing operations 
                 System.out.println ( "whether to edit the element:" + B); 
                 
                 String S = driver.findElementById ( "kW").         getTagName();         //Look at the label name 
                 System.out.println ( "kw label name:" + S);     
                 
                 String w . = Driver.findElementById ( "su") getTagName ();         // check the label name 
                 System.out.println ( "su label name : "+ W);     
                 
                 String P = driver.findElementById (" kW. ") the getAttribute (" class ");         // attribute value of the element for obtaining 
                 System.out.println (" kw class attribute value name: "+ P);     
                 
                 
                 String G = driver.findElementByPartialLinkText ( "FIG." ) .getText (); 
                 System.out.println ( "connection map text character values:" + G);     
                 
            
                 the Thread.sleep(10000);
                 driver.findElementById("kw").sendKeys("中国");
                 Thread.sleep(10000);
                 driver.findElementById("kw").clear();
                 Thread.sleep(10000);
                 driver.findElementById("kw").sendKeys("美国");
                 driver.findElementById("su").click();

                 Thread.sleep(10000);
                 
                 
            }catch (Exception e) 
            {
                e.printStackTrace();
            }finally 
            {
                driver.quit();
            
             }
       }    
    
    
}

 

Results of the:

 

Element is visible: true
whether the element editor: true
kw label name: the INPUT
su label name: the INPUT
su tag name: s_ipt
connected text text value map: Map

 

 

 

=====================================================================================================================

 

 

(1), the standard wording of
the standard wording for the tag name // [@ attribute name = "attribute value"], for example, we are looking for hot spots that element title search, then xpath was written // div [@ title = 'Search hot '] (where the value of a single attribute or double quotes may be, meaning // Find the start of the global element);

 

 

(2) to acquire the parent element
to get the parent element, xpath statement to add / .. can, so to multi-level parent element, it can be a few more, such as we get to the top of the image above the div element, which can be written xpath // div [@ title = 'Search hot'] /../../ ..;

 

 

(3) to obtain siblings
get sibling, this will be the first to locate its parent element, and then look for lower levels, such as yellow on the map to identify that element, xpath can write // div [@ title = 'Search hot spots'] /../table;

 

 

(4), multi-attribute positioned
Sometimes we still have to find a single attribute repeating elements, then how to do it, in fact, we can add more attributes, such as the above figure that a label element, it can be so xpath write // a [@ title = 'cottage Nicholas Tse fan club'] [text () = 'cottage Nicholas Tse fan club'], note that [text () = 'cottage Nicholas Tse fan club'] proprietary wording of the text;

 

 

(5), obtaining the sub-element
child element acquiring, if the sub-elements is relatively simple, can be added directly after the element [n] can, for example, to obtain a second image above td tag element, you can write the xpath is // a [@ title = 'cottage fans will Nicholas'] [text () = 'cottage fans will Nicholas'] /../../../ td [2], Note that this td [2] in 2 that their numbers, here are numbered starting with 1, if it is the first td behind that you can not add [1];

In fact, there is a simple method, that is, in chrome developer tools, select the elements you want to target, right click and choose copy Xpath can get to its xpath, but sometimes this xpath will be very long ... so the proposal is still to write better;
more that 5 is to be flexible with use.

 

 

8, look through css selectors
and find similar by xpath, this can also click directly in the chrome elements right choice cssSelector want to target to get its css selectors, but as xpath and sometimes very long ... better to write your own simple (Note that sometimes the way css selectors to write is much simpler than xpath, this recommendation learn under);

 

 

 

This part of the detailed tutorial can refer to this link: http: //www.w3school.com.cn/cssref/css_selectors.asp

 

 

 

 

========================================================================================================================

 

 


Following is a brief talk at common methods:

(1), by way id


Here selector can be written with a tag or name #id_value manner tag_name # id_value, wherein tag_name is similar to that div, id_value is the value of the id attribute of the element;

 

 

(2), by way class
here .class_value selector can be written as the name of the tag tape mode or tag_name.class_value, class_value here is the value of the class attribute value of the element;

 

 

(3) by way of other properties
can be written as herein selector [attr_name = "attr_value"] or [attr_name = 'attr_value'], this embodiment may take a tag name, immediately in front of the brackets can be;

 

 

(4), by way of tag_name
here tag_name selector is written, so you can get directly to all tag named tag_name elements, with the result tagName Find ways;

 

 

(5), by way of a path
here is written selector tag_name> tag_name, of course, be tag_name with attributes, the path may have multiple layers;

 

 

Described above can be used together in several ways;

 

 

 

 

===============================================================================================================

 

 

Third, the basic operation of elements


1, click
code element.click (), this click operation for all operations that an element can not be clicked with the click operation Throws;

 

2, clear
code element.clear (), this operation is used to input an input box, it is recommended to clear, the cause to prevent the data contents of the input character string inputted before the abnormality;

 

3, the transmission data
code element.sendKeys (keys), the operation input box for input;

 

4, submit data
code element.submit (), commonly used in this form submit button element pieces, and in general with the use of data transmission, the more common block log;

 

5, acquires attribute
code element.getAttribute (attr_key), for obtaining the attribute value of the element, attr_key key value for the attribute;

 

6, the value of obtaining css
code element.getCssValue (css_key), for a css value acquisition element, css_key is css attribute name;

 

7, acquires the position
code element.getLocation (), a position information acquisition element, i.e. the element left corner of the entire browser;

 

8, the size of the acquisition
code element.getSize (), information for obtaining the element size, i.e., length and width of the information element, an element used in the theme, and with the use of acquisition position, the element pieces may be acquired three other points coordinate;

 

9, obtaining tag_name
code element.getTagName () ;, for obtaining the element tag names;

 

10, acquires the content
code element.getText (), obtaining a value of text elements;

 

11, find an element
code element.findElement (By), according to the origin of the acquired element is an element, here less common;

 

12, whether the display
code element.isDisplayed (), returns whether to display the element, the element corresponding to this attribute style = "display: block;" ;

 

13, if the enable
code element.isEnable (), and the like commonly used in a single box;

 

14, has been selected
code element.isSelected (), to select this element pieces.

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12210085.html