Selenium+python for Web Automation testing (DEMO+API)

Source: Internet
Author: User

Selenium official website

http://selenium-python.readthedocs.io/

Configuring the Usage environment

Download the appropriate browser driver, Firefox is the default
This article is based on Chrome, placed in the scripts directory
Chromedriver official: All versions of Chromedriver

Document reference

A Concise Python tutorial

Python Tutorial-Liaoche

Official document: Selenium with Python

Webdriver Practical Guide Python version

A simple test demo
1 #Coding=utf-82  fromSeleniumImportWebdriver3 ImportOS4 Import Time5 #set little time stop and big time stop for viewing changes6Little_time_stop = 17Big_time_stop = 28 #default number of ad bars9Ads_num_require = 8Ten #Request Connection OneReq_url ="http://www.haosou.com/s?ie=utf-8&shb=1&src=360sou_newhome&q=%E9%B2%9C%E8%8A%B1" A #Open Browser -  -Browser =Webdriver. Chrome () the #Start Request -17browser.get (Req_url) - #get all the ads -  +All_ads_li = Browser.find_elements_by_css_selector ('#e_idea_pp Li') - #current number of ad bars +Ads_num_current =Len (all_ads_li) A Print "Has been got%d ads"%(ads_num_current) at #if the number of ad bars does not match the default - ifAds_num_current <Ads_num_require: -     Print "The number of ads is not enough (current:%d require:%d)"%(Ads_num_current,ads_num_require) -     #exit () - #Get top Connection -i =0 in  forAds_liinchAll_ads_li: - time.sleep (big_time_stop) toi = i+1 +     Print "Ads%d:"%I -     Try: theMain = Ads_li.find_element_by_css_selector ('H3 a') *     except: $         Print "\terror:ads%d Cann ' t find"%(i)Panax Notoginseng     Else: -         Print "\tready:visit Ads%d"%(i) the Main.click () +         Print "\tsucess:visit Ads%d"%(i) A time.sleep (little_time_stop) the     Try: +Img_link = Ads_li.find_element_by_class_name ('e_biyi_img') -     except: $         Print "\terror:no img in ads%d"%(i) $     Else: -         Print "\tready:visit Img_link%d"%(i) - Img_link.click () the         Print "\tsuccess:visit Img_link%d"%(i) - time.sleep (little_time_stop)Wuyi     Try: theChild_div = Ads_li.find_element_by_class_name ('E_biyi_childlink'); -     except: Wu         Print "\terror:no Child link in ads%d"%(i) -     Else: About         Try: $Child_links = Child_div.find_elements_by_css_selector ('a') -         except: -             Print "\terror:find child_links Error" -         Else: ANum_links =Len (child_links) +             Print "\tsuccess:there is%d child_links"%(num_links) thej =0 -              forChild_ainchchild_links: $j = j + 1 the                 Print "\t\tready:visit Child link%d in ads%d"%(J, i) the Child_a.click () the                 Print "\t\tsuccess:visit Child link%d in ads%d"%(J, i) the time.sleep (little_time_stop) - Print "End and thanks for your using!" in #The following code selects uncomment the #Delay the #Time.sleep (5) About #Close the current window the #browser.close () the #close all windows that are already open the #browser.quit ()
Positioning operations

The following are the APIs for all targeting operations:

Returns a matching element, which is a webelement element
FIND_ELEMENT_BY_ID ()
Find_element_by_name ()
Find_element_by_class_name ()
Find_element_by_tag_name ()
Find_element_by_link_text ()
Find_element_by_partial_link_text ()
Find_element_by_xpath ()
Find_element_by_css_selector ()
Returns a list containing all matching elements, i.e. a webelement list
FIND_ELEMENTS_BY_ID ()
Find_elements_by_name ()
Find_elements_by_class_name ()
Find_elements_by_tag_name ()
Find_elements_by_link_text ()
Find_elements_by_partial_link_text ()
Find_elements_by_xpath ()
Find_elements_by_css_selector ()

For a detailed description of the API, please refer to:
Module-selenium.webdriver.chrome.webdriver

The following points should be taken into consideration when using:

    • Python selenium provides two types of objects: WebDriverWebElement

Both of these objects can use these APIs

    • Once these APIs fail to execute (that is, they are not found), an exception is thrown

The mechanism must therefore be used try: .. except: ... to prevent the wrong behavior from affecting the program to continue

    • WebDriverCall the above API for global positioning

WebElementCalling the above API allows for hierarchical positioning, i.e.查找当前元素的子元素

Use of Webdriver

At the beginning, you have to initialize an WebDriver instance, that is, the following to browser control the browser and access to the page

    1. # 请求连接
    2. req_url = "http://www.haosou.com/s?ie=utf-8&shb=1&src=360sou_newhome&q=%E9%B2%9C%E8%8A%B1"
    3. # 打开浏览器
    4. browser = webdriver.Chrome()
    5. # 开始请求
    6. browser.get(req_url)

Next, you can use the above API for global lookups of the current page, such as:

    1. # 获取所有的广告
    2. all_ads_li = browser.find_elements_by_css_selector(‘#e_idea_pp li‘)
Use of Webelement
    1. Used browser.find_element_by_xx() to get an WebElement instance
      Use the obtained instance to invoke the above API for hierarchical lookups

    2. Used browser.find_elements_by_xx() to get an element that is an WebElement instance of the list
      By iterating through the list, each instance can be manipulated accordingly.

Some common operations of Webdriver
  1. browser.curren_url: Gets the URL of the currently loaded page
  2. browser.close(): Closes the current window, if the current window is the last window, the browser will close
  3. browser.quit(): Closes all windows and stops execution of Chromedriver
  4. browser.add_cookie(cookie_dict): Add a cookie for the current session
    browser.get_cookie(name): Get Execution cookie
    browser.get_cookies(): Get all the cookies

    driver.add_cookie({‘name’ : ‘foo’, ‘value’ : ‘bar’}) driver.add_cookie({‘name’ : ‘foo’, ‘value’ : ‘bar’, ‘path’ : ‘/’}) driver.add_cookie({‘name’ : ‘foo’, ‘value’ : ‘bar’, ‘path’ : ‘/’, ‘secure’:True})
  5. browser.delete_all_cookies(): Delete all cookies for the current session
    browser.delete_cookie(name): Delete the specified cookie

  6. browser.back(): Equivalent to the browser's fallback history
  7. browser.forward(): Equivalent to the browser's forward history
  8. browser.execute_script(script, *args): Synchronous execution of JS script
    browser.execute_async_script(script, *args): Executes the JS script asynchronously
  9. browser.get(url): Loads the URL in the current window
  10. browser.refresh(): Refreshes the current page
  11. browser.current_window_handle: The handle of the current window, which is the same as a pointer, to point to the current window
  12. browser.window_handles: All windows that are already open in the current browser are a list
  13. browser.switch_to_window(window_handle): Toggles the window that window_handle points to
  14. browser.title: The title of the current page
  15. browser.name: The name of the current browser

Specific reference: Module-selenium.webdriver.remote.webdriver

Some common operations of webelement
    1. webEle.clear(): Clear the contents of the element, if this element is a text element
    2. webEle.click(): Click the current element
    3. webEle,is_displayed(): Whether the current element is visible
    4. webEle.is_enabled(): Whether the current element is prohibited, such as often disabling the click of some elements
    5. webEle.is_selected(): Whether the current element is selected, the contents of the Text input box
    6. webEle.send_keys(*value): Simulates a keyboard event to the current element
    7. webEle.submit(): Submit Form
    8. webEle.tag_name: The label name of the current element
    9. webEle.text: The contents of the current element
    10. webEle.get_attribute(name): Gets the value of the current element execution property

!! This article turns from https://www.zybuluo.com/mwumli/note/222253

Selenium+python for Web Automation testing (DEMO+API)

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.