CSS Selectors: Difference between revisions
												
				Jump to navigation
				Jump to search
				
No edit summary  | 
			
(No difference) 
 | 
Latest revision as of 07:31, 2 October 2017
selectors grab onto:
tags
<a>, <p>, <div>, <table>, <tr>, <td>, <span>, <select>, <option>
tags open and close
<div>
  <p> text </p>
  <table>
    <tr>
      <td>
    </tr>
  </table>
</div>
<div class="error">
  <p> Something <span class='massive'>went</span> wrong! </p>
</div>
<br>
<p> word</p><br>
word
attributes
<a href="www.google.com">
<span class="red">
<div class="footer small">
<option value="option1">
<table id="results_table">
<input type="checkbox">
to grab a class: ".classname"
<div class='error'> css=.error
to grab an id: "#google_link"
<a id='google_link' href='www.google.com'>
css=#google_link
to grab more than one class: ".classone.classtwo"
<div class="classone classtwo">
css=.classone.classtwo
to grab an attribute: "a[href='www.google.com']"
<a href="www.google.com"></a>
to grab ALL instances of a tag: "div"
<div></div>   //grabs
  <p>
<div>         //grabs
<div></div> //grabs
</div>
to grab a tag with a specific class: "div.classname"
to grab the direct child of an element: "div > p"
  <div>
    <p> text! </p> // grabs this element
  </div>
to grab a descendent: "div p"
  <div>
    <div>
      <p> text! </p> // grabs this element
    </div>
  </div>
to grab a sibling: p + p
  <div>
    <p></p>
    <p></p> // grabs this element
  </div>
getting more specific: #first_item p
  <ul>
    <li id='first_item'>
      <div>
        <p> text </p>  //grabs
      </div>
    </li>
    <li>
      <p> text </p>   //does NOT grab. why?
    </li>
  </ul>
:nth-of-type() -- li:nth-of-type(3)
  <ul>
    <li> </li>
    <li> </li>
    <li> </li>  //grabs
    <li> </li>
  </ul>
:first-of-type  -- li:first-of-type
  <ul>
    <li> </li>  //grabs
    <li> </li>
    <li> </li>
    <li> </li>
  </ul>
:first-child -- ul > :first-child
  <ul>
    <li> </li>  //grabs
    <li> </li>
    <li> </li>
    <li> </li>
  </ul>
:nth-child() -- ul > nth-child(3)
  <ul>
    <li> </li>
    <li> </li>
    <li> </li>  //grabs
    <li> </li>
  </ul>
  :nth-child -- p:nth-child(1) -- what happens?
  <div>
    <div> </div>
    <p> </p>
    <p> </p>
    <p> </p>
  </div>
to grab a parent, ancestor: whoops! can't do that with css. we need xpath
Exercises
- grab all paragraphs on page
 - grab the -very first- paragraph on the page
 - grab the fourth element in the form. what is it?
 - what is the third element in the form? do you see it?
 - grab the option with the value 'dog'
 - find the checkbox
 - select the entire page