Selenium CSS positioning elements:
Cssselector positioning, belongs to the CSS high-level, its positioning method, using selectors. In CSS, a selector is a pattern that selects the object to which you want to add a style. The CSS column indicates which version of the CSS the property is defined in. (CSS1, CSS2 or CSS3. );
Here is a partial list of CSS positioning methods. See so much whether or not the CSS is no longer simple, it is not commonly used in several ways the author has been tagged, CSS positioning is a common use of the process is a very important way. It has many similarities to XPath positioning, but the CSS is more advantageous both in terms of performance and syntax.
1, the general situation of positioning speed than XPath faster
2. Syntax is more concise than XPath
Example:
Import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.chrome.chromedriver;public class Elementlocalization {public static void main (string[] args) { String chromepath=system.getproperty ("User.dir") + "\\driver\\chromedriver.exe"; System.setproperty ("Webdriver.chrome.driver", Chromepath); Webdriver driver=new chromedriver (); Driver.manage (). window (). Maximize (); Driver.get ("https://www.baidu.com"); try {webelement element=null; Define a Web Object object//Element=driver.findelement (By.cssselector (". S_ipt"));//css style name positioning Baidu Search Input Box Element=d River.findelement (By.cssselector ("#kw"))//cssid locate Baidu search Input Box element=driver.findelement (By.cssselector ("Div, A "));//css locates all Div objects and a object Element=driver.findelement (By.cssselector (" div a "));//css locates all a objects within the DIV tag Element=driver.findelement (By.cssselector ("Div>a")//css the parent object is the div's all a object Element=driver.findelement (By.cssselector (" Div+a "));//css locates all <a immediately after the <div> object > Object Element=driver.findelement (By.cssselector ("[target= ' _blank ']");//Select all objects of target= "_blank". Element=driver.findelement (By.cssselector ("A[name= ' tj_trnews '][class= ' Mnav ']");//positioning a tag name= ' tj_ Trnews ' and class= ' Mnav ' Object Element=driver.findelement (By.cssselector ("a[href^= ' http ')");//Select a Tag object Each <a> object in the href attribute value that begins with "http". Element=driver.findelement (By.cssselector ("a[href$="));//Select the object in the A-label object with the value of the href attribute ending with ". com" Element=driver.findelement (By.cssselector ("a[href*= ' News"));//Select an object in the HREF attribute value that contains news in the A-label object Element=driv Er.findelement (By.cssselector ("A:nth-last-child (2)");//select each <a> object that belongs to the first <a> object of its parent object. Element=driver.findelement (by.cssselector ("HTML body div#wrapper div#head div.head_wrapper Div.S_form div.s_form_wrapper.soutu-env-nomac.soutu-env-index form#form.fm span.bg.s_ipt_wr.quickdelete-wrap input# Kw.s_ipt ")),//through the CSS absolute path location} catch (Exception e) {System.out.println (" failed to locate "); } }}
Note: The above positioning method belongs to the basic use, if necessary, please do all the ways to browse again;
How to use selenium CSS positioning?