http://blog.csdn.net/qingchunjun/article/details/42581261
When you use selenium webdriver for element positioning, you typically use the Findelement or Findelements method to position the element with the element handle returned by the by class. Among them, by the common positioning method of eight kinds, are described below respectively.
1. By.name ()
Page source code is as follows: [HTML] view plain copy <button id= "GBQFBA" aria-label= "Google Search" name= "Btnk" class= "GBQFBA" > <spanidspanid= "GBQFSA" >google search</span></button>
When we use the Name property to refer to this button and click on it, the code is as follows: [Java] view plain copy public class Searchbuttonbyname {public Stati c void Main (string[] args) {Webdriver driver = new Firefoxdriver (); Driver.get ("http://www.forexample.com"); Webelement searchbox =driver.findelement (By.name ("BTNK")); Searchbox.click (); } }
2. By.id ()
The
Page source code is as follows: [HTML] View plain copy <button id= "GBQFBA" aria-label= "Googlesearch" name= "BTNK" class= "GBQFBA" ><spanidspanid= "GBQFSA" >google search</span></ button>
To reference the button and click on it, the code is as follows: [Java] view plain copy public class Searchbuttonbyid {public static void main (St Ring[] args) {Webdriver driver = new Firefoxdriver (); Driver.get ("http://www.forexample.com"); Webelement searchbox =driver.findelement (by.id ("GBQFBA")); Searchbox.click (); } }
3. By.tagname ()
The method can find the element by its label name. The difference between this method and the previous two methods is that this method usually searches for more than one element, so it is generally recommended to use the Findelements method together. For example, if we want to find out how many buttons are on the page, we can use the button tagname to find the code as follows: [Java] View plain copy