Eight common ways to locate Selenium webdriver elements

Source: Internet
Author: User
Tags tagname

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

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.