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:
WebDriver
WebElement
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
WebDriver
Call the above API for global positioning
WebElement
Calling 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
# 请求连接
req_url = "http://www.haosou.com/s?ie=utf-8&shb=1&src=360sou_newhome&q=%E9%B2%9C%E8%8A%B1"
# 打开浏览器
browser = webdriver.Chrome()
# 开始请求
browser.get(req_url)
Next, you can use the above API for global lookups of the current page, such as:
# 获取所有的广告
all_ads_li = browser.find_elements_by_css_selector(‘#e_idea_pp li‘)
Use of Webelement
Used browser.find_element_by_xx()
to get an WebElement
instance
Use the obtained instance to invoke the above API for hierarchical lookups
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
browser.curren_url
: Gets the URL of the currently loaded page
browser.close()
: Closes the current window, if the current window is the last window, the browser will close
browser.quit()
: Closes all windows and stops execution of Chromedriver
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})
browser.delete_all_cookies()
: Delete all cookies for the current session
browser.delete_cookie(name)
: Delete the specified cookie
browser.back()
: Equivalent to the browser's fallback history
browser.forward()
: Equivalent to the browser's forward history
browser.execute_script(script, *args)
: Synchronous execution of JS script
browser.execute_async_script(script, *args)
: Executes the JS script asynchronously
browser.get(url)
: Loads the URL in the current window
browser.refresh()
: Refreshes the current page
browser.current_window_handle
: The handle of the current window, which is the same as a pointer, to point to the current window
browser.window_handles
: All windows that are already open in the current browser are a list
browser.switch_to_window(window_handle)
: Toggles the window that window_handle points to
browser.title
: The title of the current page
browser.name
: The name of the current browser
Specific reference: Module-selenium.webdriver.remote.webdriver
Some common operations of webelement
webEle.clear()
: Clear the contents of the element, if this element is a text element
webEle.click()
: Click the current element
webEle,is_displayed()
: Whether the current element is visible
webEle.is_enabled()
: Whether the current element is prohibited, such as often disabling the click of some elements
webEle.is_selected()
: Whether the current element is selected, the contents of the Text input box
webEle.send_keys(*value)
: Simulates a keyboard event to the current element
webEle.submit()
: Submit Form
webEle.tag_name
: The label name of the current element
webEle.text
: The contents of the current element
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)