Selenium is simple to use python reptiles

A, Selenium basis Introduction and Installation

      1, Selenium Introduction

         Selenium is a test site for the automated testing tool that supports a variety of browsers including Chrome, Firefox, Safari and other mainstream browser interface, also supports phantomJS no browser interface.

     2, installation Selenium

pip install Selenium

     3, install the browser driver 

                When selenium upgrade to 3.0 for different browsers drives were the norm. If you want to use a different browser selenium drive, you must be downloaded separately and set a different browser driven.

          Each browser Download:                    

                Firefox browser driver: geckodriver

               Chrome browser driver: chromedriver 

               IE browser driver: IEDriverServer

               Edge Browser drive: MicrosoftWebDriver

               Opera browser driver: operadriver

               PhantomJS browser driver: PhantomJS

               Note: Some browsers need to drive address outside the network. Configure the browser windows driven environment

                   After the download, unzip ubuntu environment, into / usr / bin

Two, Selenium Getting Started  

     1, selenium elements positioned usage

               If we have a Web page, view the properties of an element is by front-end tools (eg, Firebug).               

<html>
  <head>
  <body link="#0000cc">
    <a id="result_logo" href="/" onmousedown="return c({'fm':'tab','tab':'logo'})">
    <form id="form" class="fm" name="f" action="/s">
      <span class="soutu-btn"></span>
        <input id="kw" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off">
    <form/>
  <body/>
  <head/>
<html/>

Our aim is to position the input box input the label.

  • By positioning id:
dr.find_element_by_id("kw")
  • Positioning by name:
dr.find_element_by_name("wd")
  • name located by class:
dr.find_element_by_class_name("s_ipt")
  • By positioning tag name:
dr.find_element_by_tag_name("input")
  • By positioning xpath, positioned xpath N kinds of writing, writing several commonly used are listed here:
dr.find_element_by_xpath("//*[@id='kw']")
dr.find_element_by_xpath("//*[@name='wd']")
dr.find_element_by_xpath("//input[@class='s_ipt']")
dr.find_element_by_xpath("/html/body/form/span/input")
dr.find_element_by_xpath("//span[@class='soutu-btn']/input")
dr.find_element_by_xpath("//form[@id='form']/span/input")
dr.find_element_by_xpath("//input[@id='kw' and @name='wd']")
  • By positioning css, css positioned N kinds of writing, writing several commonly used are listed here:
dr.find_element_by_css_selector("#kw")
dr.find_element_by_css_selector("[name=wd]")
dr.find_element_by_css_selector(".s_ipt")
dr.find_element_by_css_selector("html > body > form > span > input")
dr.find_element_by_css_selector("span.soutu-btn> input#kw")
dr.find_element_by_css_selector("form#form > span > input")

Next, there is a set of text links on our pages.

<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>
<a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
  • By locating text link:
dr.find_element_by_link_text ( " News " )
dr.find_element_by_link_text("hao123")
  • By locating text link:
dr.find_element_by_partial_link_text("")
dr.find_element_by_partial_link_text("hao")
dr.find_element_by_partial_link_text("123")

 

Guess you like

Origin www.cnblogs.com/talented-stefan/p/11627503.html