table of Contents
1, the statement browser object
First, the installation
1, pip3 install selenium # Note that the new version of the label is no longer supported phantomjs
2, the browser downloads each drive (for example with chrome)
(1) Chrome need to download chromedriver
Download: http: //chromedriver.storage.googleapis.com/index.html
(2) Add the environment variable to the file directory
mac和linux :sudo mv chromedriver /usr/local/bin/chromedriver
(3) Verify: bath input chromedriver
Second, the use
1, the statement browser object
from the Selenium webdriver Import
Browser = webdriver.Chrome ()
Browser = webdriver.Firefox ()
Browser = webdriver.Edge ()
Browser = webdriver.Safari ()
Browser = webdriver.PhantomJS () # abandoned by
rewriting file_detector_context # detector current file
name # browser name
start_client () # access before the start
stop_client () # after the end of the visit
start_session () # create a new session with the desired function.
create_web_element () # Create a label specified element_id
execute () # execute command
get () # load the page
title # current page title
find_element. . . . . . # Find node
execute_script () # synchronous execution Js
execute_async_script () # asynchronous execution
current_url # current url
page_source # page source
close () # close
quit () # exit the driver and close all windows.
current_window_handle # current window handle
window_handles # window handle all
maximize_window () # maximized window
fullscreen_window () # fullscreen
minimize_window # window is minimized
switch_to # = driver.switch_to.active_element switching Element
Alert = driver.switch_to.alert
driver.switch_to.default_content ( )
driver.switch_to.frame ( 'frame_name')
driver.switch_to.frame (. 1)
driver.switch_to.frame (driver.find_elements_by_tag_name ( "iframes") [0])
driver.switch_to.parent_frame ()
driver.switch_to.window ( 'main')
the Back () # backward
forward () # forward
refresh () # refresh
get_cookies () # get the current Cookies
get_cookie (name) # get a single cookie by name. If found return cookie, otherwise none.
delete_cookie (name) # delete the cookie
delete_all_cookies () # Empty the cookie
add_cookie (diect) # increase the cookie
implicitly_wait (Time) # implicit wait
set_script_timeout (wait_time) # set the script execution \ Asynchronous \ script called before the error is raised.
set_page_load_timeout (wait_time) # Set the time to wait for page loads before throwing error.
desired_capabilities # Returns the driver is using the current required functions
get_screenshot_as_file (filename) # will be saved as a screenshot file
save_screenshot (filename) # The current window screenshot is saved to PNG image file
get_screenshot_as_png () # shots, binary
get_screenshot_as_base64 ( ) # free, Base64
set_window_size (width, height, windowHandle = 'Current') # set window size
get_window_size () # acquisition window size
set_window_position (x, y) # settings window position
get_window_position () # acquisition window position
get_window_rect () # Get window x, y coordinates and the current window.
set_window_rect () # settings window x, y coordinates
file_detector # document detector set sending keyboard input to be used.
The current direction setting device orientation #
driver.orientation = 'Landscape'
allowed_values = [ 'LANDSCAPE The', 'PORTRAIT']
application_cache # Returns the target application and interact with the cache applicationcache browser
get_log (log_tpye) # log type list of available
2, page visits
start with the browser 'Chrome being controlled automatic software' questions:
from selenium import webdriver
option = webdriver.ChromeOptions() #加载启动配置
option.add_argument('disable-infobars')
browser = webdriver.Chrome(chrome_options=option)
browser.get('https://www.taobao.com')
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
page = browser.page_source # <class 'str'>
print(page,type(page),sep='\n')
browser.close()
3、查找节点
(1)单个节点
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_1 = browser.find_element_by_id('q')
input_2 = browser.find_element_by_css_selector('#q')
input_3 = browser.find_element_by_xpath('//*[@id="q"]')
print(input_1,input_2,input_3,sep='\n')
browser.close()
三种方法殊途同归:
<selenium.webdriver.remote.webelement.WebElement", element="0.652580522307187-1")>
<selenium.webdriver.remote.webelement.WebElement ", element="0.652580522307187-1")>
<selenium.webdriver.remote.webelement.WebElement", element="0.652580522307187-1")>
All methods
browser.find_element_by_id()
browser.find_element_by_name()
browser.find_element_by_xpath()
browser.find_element_by_css_selector()
browser.find_element_by_class_name()
browser.find_element_by_link_text()
browser.find_element_by_tag_name()
browser.find_element_by_partial_link_text()
通用方法:find_element()
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_1 = browser.find_element(By.ID,'q')
print(input_1)
browser.close()
<selenium.webdriver.remote.webelement.WebElement (session="xxx", element="0.13972884174276556-1")>
(2) multi-node
The method is similar to the single node, more than s, the result obtained is a list of WebElement.
find_elements()
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_1 = browser.find_elements(By.CSS_SELECTOR,'.service-bd li')
print(input_1)
browser.close()
其他方法
browser.find_elements_by_id()
browser.find_elements_by_name()
browser.find_elements_by_xpath()
browser.find_elements_by_css_selector()
browser.find_elements_by_class_name()
browser.find_elements_by_link_text()
browser.find_elements_by_tag_name()
browser.find_elements_by_partial_link_text()
(3) node interaction
Common methods: send_keys (): Enter the text, clear (): Empty text, click (): Click
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
inputs = browser.find_element_by_id('kw')
inputs.send_keys('iphone')
time.sleep(1)
inputs.clear()
inputs.send_keys('ipad')
button = browser.find_element_by_id('su')
button.click()
4、动作链
from selenium import webdriver
from selenium.webdriver import ActionChains
browser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult')
source = browser.find_element_by_css_selector('#draggable')
target = browser.find_element_by_css_selector('#droppable')
actions = ActionChains(browser) #声明对象
actions.drag_and_drop(source,target)
actions.perform() #执行
5、执行js代码
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
browser.execute_script('alert("To Btottom")')
The main method of the object under WebElement
tag_name # label name
text # label text
click () # Click this tab
submit () # submit the form
clear () # If it is a text input element, clear text
get_property (name) # Gets the label given property
get_attribute (name) # ibid. found no difference
is_selected () # returns whether the selected element. Can be used to check whether the check box or radio buttons.
is_enabled () # Returns whether the element is enabled
send_keys () # analog input element.
is_displayed () # is visible
location_once_scrolled_into_view #
size # size elements
value_of_css_property (name) #css attribute value of
the position of elements may be rendered location # canvas
rect # dictionary with the size and position of the element.
screenshot_as_base64 # take a screenshot as a base64 encoded string of the current element.
= element.screenshot_as_base64 img_b64
screenshot_as_png # Get the current element as the binary data screenshot
screenshot (filename) # save the screenshot, returns a Boolean value
parent # parent
internal id # used to detect whether an object agree
6, switching Frame
iframe node, equivalent to a sub-page of the page. Selenium default action for the parent page. Enter the sub page by switch_to.frame ()
browser.get(url)
brower.swich_to.frame('iframeResult')
7, delay waiting for
the implicit wait: did not find the node will wait for a certain time to go look for implicitly_wait ()
Selenium the webdriver Import from
Browser = webdriver.Chrome ()
browser.implicitly_wait (10)
browser.get ( 'https://www.zhihu.com/explore')
Inputs = browser.find_element_by_class_name ( 'the Add-zu-Top-Question' )
Print (the Inputs)
shows the wait: specify a maximum waiting time, if you load up within the specified time to return to find nodes; to load the specified time is still not out, then throw timeout exception.
Import the webdriver Selenium from
from selenium.webdriver.common.by By Import
from selenium.webdriver.support.ui Import WebDriverWait
from selenium.webdriver.support Import expected_conditions AS EC
Browser = webdriver.Chrome ()
browser.get ( 'HTTPS: // www.taobao.com ')
the wait = WebDriverWait (Browser, 10) example # object
Inputs = wait.until (EC.presence_of_element_located ((By.ID,' Q ')))
Button = wait.until (EC.element_to_be_clickable ( (By.CSS_SELECTOR, '. BTN-Search')))
Print (Inputs, Button, On Sep = '\ n-')
title_is is a content title #
title_contains # header includes a content
presence_of_element_located # dotted out loading incoming tuple positioned
url_contains
url_matches
url_to_be
url_changes
visibility_of_element_located # Node incoming visible positioning tuple
visibility_of # Node incoming visible objects
presence_of_all_elements_located # loading all the nodes out
visibility_of_any_elements_located
visibility_of_all_elements_located
text_to_be_present_in_element # a node comprising a text character
text_to_be_present_in_element_value # a node value contains a character
frame_to_be_available_and_switch_to_it # loads and switching
invisibility_of_element_located # node invisible
invisibility_of_element
element_to_be_clickable # node can click
staleness_of # judge whether a node is still DOM, can be used to determine the refresh
element_to_be_selected # node to select the incoming node object
element_located_to_be_selected # node may select, locate incoming tuple
element_selection_state_to_be # node object and passed state, equal returns true, otherwise false
element_located_selection_state_to_be # tuple incoming positioning and status, equal returns true, otherwise false
number_of_windows_to_be
new_window_is_opened
alert_is_present # warning appears
8, forward and backward
browser forward and back function --back () / forward ()
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.get('https://www.taobao.com')
browser.get('https:..www.zhihu.com')
browser.back()
time.sleep(1)
browser.forward()
9、Cookies
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
print(browser.get_cookies())
browser.add_cookie({'name':'name','domain':'www.zhihu.com','value':'sdfwer'})
#cookie至少需要name和value两个key。
print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies ())
10, the operation tab
webdriver the Selenium Import from
Import Time
Browser = webdriver.Chrome ()
browser.get ( 'https://www.baidu.com')
browser.execute_script ( 'window.open ()') # open a new page in the tab
browser. switch_to.window (browser.window_handles [1]) # handover
browser.get ( 'https://www.taobao.com')
browser.switch_to.window (browser.window_handles [0])
# browser.close () # Close this page
the time.sleep (1)
browser.quit () # close all pages and exiting
11, exception handling
from the Selenium Import webdriver
from selenium.common.exceptions Import TimeoutException, NoSuchElementException
Browser = webdriver.Chrome ()
the try:
browser.get ( 'HTTPS: www.baidu.com')
the except a TimeoutException:
Print ( 'Time OUT')
the try:
browser.find_element_by_class_name('lsd')
except NoSuchElementException:
print('No such elemnt')
finally:
browser.close()
----------------
Disclaimer: This article is CSDN blogger original article "Marvin_Wind", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/Marvin_Wind/article/details/86004937