1.1.1 Browser window size set_windows_size()
# coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://news.baidu.com/")
driver.set_window_size(480,800) #Set the browser size to mobile style
driver.maximize_window() #Set the browser to display full screen
1.1.2 Browser forward and backward
driver.back()
driver.forward()
1.1.3 Browser refresh
driver.refresh()
1.2 Simple element manipulation
clear() #clear
send_keys("value") #input value
click() #Click (buttons, pictures, checkboxes, radio buttons, drop-down boxes, etc.)
1.2.1 Common methods of WebElement interface
u submit() #Submit the form
u element.size #return element size
u element.text #Get element text
u element.get_attribute(name) #Get the name attribute value such as: .get_attribute('type')
u element.is_displayed #Returns whether the element is visible to the user (visible: True)
1.3 Mouse events
The method of mouse operation is encapsulated in the ActionChains class.
Introduce ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
u perform() executes all actions stored in ActionChains
u context_click() right click
u double_click() double click
u move_to_element() mouseover
u drag_and_drop() drag
E.g:
元素a = driver.find_element_by_id(‘xx’)
元素b = driver.find_element_by_id(‘ww’)
1.3.1 Right-click a
ActionChains(driver).context_click(a).perform()
1.3.2 Double-click a
ActionChains(driver). double_click(a).perform()
1.3.3 Hover a
ActionChains(driver). move_to_element(a).perform()
1.3.4 Drag a to b
ActionChains(driver). drag_and_drop(a,b).perform()
1.4 Keyboard events
#Introduce keyboard events
from selenium.webdriver.common.keys import keys
Common operations:
Send_keys(keys.BACK_SPACE) delete key
send_keys(Keys.SPACE) space key
send_keys(Keys.TAB) Tab keys
send_keys(Keys.ESPACE) fallback key
send_keys(Keys.ENTER) Enter key
send_keys(Keys.CONTROL,'a') 全选
send_keys(Keys.CONTROL,'c') copy
send_keys(Keys.CONTROL,'x') cut
send_keys(Keys.CONTROL,'v') paste
send_keys(Keys.F1) F1
1.5 Obtaining verification information
Get title, url, or text for verification.
- driver.title
- driver.current_url
- element.text
1.6 Set element wait
1.6.1 Display waiting
The webdriver waits for a certain condition to continue to execute, otherwise it throws a TimeoutException when the maximum duration is reached.
# Introduce element wait method
from selenium.webdriver.support.ui import WebDriverWait
The WebDriverWait class is a waiting method provided by webdriver. During the set time, it detects whether the current page element exists at regular intervals by default. If the element is detected beyond the set time, an exception will be thrown. The syntax is as follows:
WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
- driver: browser driver
- timeout: timeout, in seconds
- poll_frequency: the interval of detection, the default is 0.5s
- ignored_exceptions: throws an exception after timeout, throws NoSuchElementException by default.
WebDriverWait() is generally used in conjunction with until() or until_not().
² until(method,massage''): Call the method provided with the driver as a parameter until the return value is True
² until_not(method,massage''): Call the method provided with the driver as a parameter until the return value is False
1.6.1.1 expected_conditions
The expected condition judgment method provided by the expected_conditions class
u title_is determines whether the title of the current page is equal to the expected
u title_contains determines whether the current page title contains the expected string
u presence_of_element_located judges whether the element is added to the DOM tree, it does not mean that the element must be visible
u visbility_of_element_located determines whether the element is visible (visible means that the element is not hidden, and the width and height of the element are not equal to 0)
u visbility_of has the same function as the previous method, but receives different parameters. The last received parameter is positioning, this parameter is the positioned element
u presence_of_all_elements_located determines whether at least one element exists in the DOM tree. Return True as long as there is 1
u text_to_be_present_in_element determines whether the text in an element contains the expected string
u text_to_be_present_in_element_value determines whether the value attribute of an element contains the expected string
u frame_to_be_available_and_switch_to_it Determine whether the form can be switched in, if yes, return True and switch in, otherwise return False
u invisbility_of_element_located determines whether an element does not exist in the DOM tree or is not visible
u element_to_be_clickable() determines whether an element is visible and clickable
u staleness_of Wait until an element is removed from the DOM tree, to determine whether an element is still in the DOM, and to determine whether the page has been refreshed
u element_to_be_selected Determines whether an element is selected, generally used in drop-down lists
u element_selection_state_to_be Determine whether the selected state of an element is as expected, pass in the element object and state, return True if they are equal, otherwise return False
u element_located_selection_state_to_be has the same function as the previous method, except that the previous method parameter is the positioned element, and the parameter received by this method is the positioning
u alert_is_present to determine whether there is an alert on the page
E.g:
Ele = WebDriverWait(driver, 5,0.5).until(EC. presence_of_element_located(By.ID,’wk’))
1.6.1.2 is_displayed
for i in range(10):
try:
el = driver.find_element_by_id("s_btn_wr")
if el.is_displayed():
break
except:pass
sleep(1)
else:
print('time out')
1.6.2 Implicit Waits
Wait for the page element to load for a set period of time. If the loading has not been completed after the set time, a NoSuchElementException will be thrown. webdriver provides implicitly_wait() method to implement implicit wait, which is set to 0 by default.
# coding=utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from time import ctime
driver = webdriver.Chrome()
driver.implicitly_wait(10) #Wait for 10 seconds
driver.get("http://news.baidu.com/")
try:
print(ctime())
driver.find_element_by_id("ww").send_keys("selenium2")
except NoSuchElementException as e:
print (s)
finally:
print(ctime())
driver.quit()
1.6.3 sleep hibernation
# Introduce the sleep() method
from time import sleep
sleep(2) fixed sleep for 2 seconds