Selenium's WebDriver API summary

A, Web Driver principle

WebDriver in accordance with the Client / Server model design classic design.

simply put:

Client-side is our test script, send http request to test the browser;

Server-side browser is arbitrary, the Remote Client-Server receives the request to respond to and, in return execution status Reponse, the return value information

  • WebDriver specific workflow:

  1. WebDriver start the target browser and bind to the specified port. Start browser as a remote server Remote Server
  2. Client sends a request via http to CommandExecuter remote server listening port (communication protocols: the webdriver wire protocal)
  3. Remote Server remote server need to rely on native browser components (such as: IEDriverServer.exe, chromedriver.exe, etc.) into a browser to a local (native) call
  • WebDriver used protocols :

  1. When you open the browser: HTTP protocol
  2. Client sends an http request to the remote server listening port: the webdriver wire protocol

       among them:

  • Wired protocol : refers to a way to get data from point to point, it is the application layer protocol.
  • HTTP protocol : the transmission from the server is an HTML Hypertext Markup Language protocol to the client. It is an application layer protocol, the request / response structure, is a standard client / server model. It is a stateless protocol. (Stateless: no memory capability of the transaction, will not save the information transmitted - save memory)

Two, WebDriver common method of operation and

1, the positioning method element

Selenium provides 8 kinds of targeting:

 

For the above method to do the following instructions:

  • Positioning id: specified in HTML id attributes in the HTML document must be unique, similar to the civil identification numbers, and highly unique.
  • Location name: name to specify the name of the element, the attribute value of the name of the current page may not be unique.
  • class orientation: Name of the class specified elements.
  • tag positioning: the nature of HTML is to define the different functions by tag, on each element of nature is also a tag. Because a tag is often used to define a class function, so the probability tag identifying an element is low.
  • link positioning: designed to position the text links. By locating text information elements between element tag pair.
  • partial link positioning: a complement to link positioning, taking into account some text links rather long, so take part positioning, as long as you can uniquely identify the link.

In a real project, sometimes an element and no id, the name attribute, or multiple page elements id, the same name attribute value, or refresh the page every time, id values ​​are random changes, for these situations, selenium in the following manner to locate:

  • XPath location: XPath is a language for addressing elements in the XML document. HTML can be seen as an implementation of XML.
  • Positioning absolute path: uses a hierarchical relationship tag name of the elements to locate the absolute path  
1 find_element_by_xpath("/html/body/div/div[2]/div/div/div/from/span/input")
  • Using positioning element attributes:
1 find_element_by_xpath("//input[@id='kw']")
2 find_element_by_xpath("//input[@name='wd']")
3 find_element_by_xpath("//*[@id='kw']")
  • Combined with the level of property
1 find_element_by_xpath("//span[@class='bg_s_ipt_wr']/input")
2 find_element_by_xpath("//form[@id='fprm']/span/input")
  • Using Logical Operators
find_element_by_xpath("//input[@id='kw' and @class='su']/span/input")
  • CSS positioning:
  • By using targeting elements: In addition to the above eight kinds of targeting methods further provide a unified find_element call () method, to declare By positioning method, positioning and pass corresponding to the positioning method parameter, two parameters, the type of positioning, targeting specific the way. In fact, the underlying is above eight methods
find_element(By.ID,“kw”)
find_element(By.NAME,“wd”)
find_element(By.CLASS_NAME,“s_ipt”)
find_element(By.TAG_NAME,“input”)
find_element(By.LINK_TEXT,“新闻”)
find_element(By.PARTIAL_LINK_TEXT,“新”)
find_element(By.XPATH,"//[@class='bg s_btn']")
find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")

 

Guess you like

Origin www.cnblogs.com/sone-1994/p/11366693.html