定位UI元素 (WebElements)

关于元素的选取,有如下的API

  • 单个元素选取
    find_element_by_id
    find_element_by_name
    find_element_by_xpath
    find_element_by_link_text
    find_element_by_partial_link_text
    find_element_by_tag_name
    find_element_by_class_name
    find_element_by_css_selector
    
  • 多个元素选取

    find_elements_by_name
    find_elements_by_xpath
    find_elements_by_link_text
    find_elements_by_partial_link_text
    find_elements_by_tag_name
    find_elements_by_class_name
    find_elements_by_css_selector
    
  • By Id

    For instance, consider this page source:

      <html>
       <body>
        <form id="loginForm">
         <input name="username" type="text" />
         <input name="password" type="password" />
         <input name="continue" type="submit" value="Login" />
        </form>
       </body>
      <html>
    

    The form element can be located like this:

login_form = driver.find_element_by_id('loginForm')
  • By Name

    For instance, consider this page source:

      <html>
       <body>
        <form id="loginForm">
         <input name="username" type="text" />
         <input name="password" type="password" />
         <input name="continue" type="submit" value="Login" />
         <input name="continue" type="button" value="Clear" />
        </form>
      </body>
      <html>
    

    The username & password elements can be located like this:

username = driver.find_element_by_name('username') password = driver.find_element_by_name('password')
  • By XPath

    This is a little abstract, so for the following piece of HTML:

      <html>
       <body>
        <form id="loginForm">
         <input name="username" type="text" />
         <input name="password" type="password" />
         <input name="continue" type="submit" value="Login" />
         <input name="continue" type="button" value="Clear" />
        </form>
      </body>
      <html>
    

    The form elements can be located like this:

login_form = driver.find_element_by_xpath("/html/body/form[1]") login_form = driver.find_element_by_xpath("//form[1]") login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
  • By (partial) Link Text

    For instance, consider this page source:

      <html>
       <body>
        <p>Are you sure you want to do this?</p>
        <a href="continue.html">Continue</a>
        <a href="cancel.html">Cancel</a>
      </body>
      <html>
    

    The continue.html link can be located like this:

continue_link = driver.find_element_by_link_text('Continue') continue_link = driver.find_element_by_partial_link_text('Conti')
  • By Tag Name

    For instance, consider this page source:

      <iframe src="..."></iframe>
    

    The iframe element can be located like this:

frame = driver.find_element_by_tag_name("iframe")
  • By Class Name

    For instance, consider this page source:

      <html>
       <body>
        <p class="content">Site content goes here.</p>
       </body>
      <html>
    

    The “p” element can be located like this:

content = driver.find_element_by_class_name('content')
  • By CSS Selectors

    Example of to find the cheese below:

      <html>
       <body>
        <p class="content">Site content goes here.</p>
       </body>
      <html>
    

    The “p” element can be located like this:

content = driver.find_element_by_css_selector('p.content')