Selenium + Java (four) Selenium Xpath element positioning Selenium + Java (three) Selenium element positioning

Foreword

About Selenium element positioning, this is the last blog post.

Xpath positioning functions can be achieved  Selenium + Java (three) Selenium element positioned in talking about targeting can also be achieved, to use that specific targeting method to be selected according to their actual situation, flexible use.

The positioning element to success is king.

A, Xpath level with the index positioning

12306 to the official website as an example: If you want to locate the contents of this li tag, content li tag if there is no corresponding attribute, then you can navigate to the first upper element div or li ul, and then looking down from the top element. You can also look for the lower element upwards, looking up and down to find little difference between the use of ".." to return to the upper element.

 

// references IE browser driver 
System.setProperty ( "webdriver.ie.driver", "./src/driver/IEDriverServer.exe" );
 // create IE browser object 
InternetExplorerDriver Driver = new new InternetExplorerDriver ();
 // Browse is maximized 
. driver.manage () window () the maximize ();.
 // wait for the browser loaded 
driver.manage () timeouts Total () implicitlyWait (10.. , TimeUnit.SECONDS);
 // open the site 
driver.get ( "https://www.12306.cn/index/" );
 // the Xpath positioning, first find the class attribute news-index div element, whereby a first div looking down li ul of standard element the elements a, if necessary to find the n-th li labels need only be modified to 1 n. 
driver.findElement (By.xpath ( "// div [ @ Class = 'news-index'] / ul / li [1] / a"));

Two, Xpath positioning properties

In Baidu Example: This may be positioned by the positioning elements with attributes (id, name, class, other attributes) tag

// references IE browser driver 
System.setProperty ( "webdriver.ie.driver", "./src/driver/IEDriverServer.exe" );
 // create IE browser object 
InternetExplorerDriver Driver = new new InternetExplorerDriver ();
 // Browse is maximized 
. driver.manage () window () the maximize ();.
 // wait for the browser loaded 
driver.manage () timeouts Total () implicitlyWait (10.. , TimeUnit.SECONDS);
 // open the site 
driver.get ( "https://www.baidu.com/" );
 // the Xpath positioned 
driver.findElement (By.xpath ( "// * [ @ id = 'kw']"));

Three, Xpath fuzzy positioning

// references IE browser driver 
System.setProperty ( "webdriver.ie.driver", "./src/driver/IEDriverServer.exe" );
 // create IE browser object 
InternetExplorerDriver Driver = new new InternetExplorerDriver ();
 // Browse is maximized 
. driver.manage () window () the maximize ();.
 // wait for the browser loaded 
driver.manage () timeouts Total () implicitlyWait (10.. , TimeUnit.SECONDS);
 // open the site 
driver.get ( "https://www.12306.cn/index/" );
 // the Xpath fuzzy positioning properties 
driver.findElement (By.xpath ( "// * [ contains (@ data-href, 'index.h')] " ));
 // the Xpath fuzzy positioned at the beginning 
driver.findElement (By.xpath (" // * [ starts-with (@id, 'search_o')]"));
 // the Xpath fuzzy positioning text 
driver.findElement (By.xpath ( "// * [ contains (text (), ' I 1230')]"));

Guess you like

Origin www.cnblogs.com/yogouo/p/11949095.html