Two object positioning
2.1 Positioning a single element
In automated testing, the positioning and operation of object elements is the core part of automation, but the object is the basis of positioning automation, and the operation of the object can be formed on the basis of the object.
Webdriver provides methods for element positioning, the common ones are as follows:
id
name
class name
tag name
link text
partial link text
xpath
css selector
The individual methods corresponding to pythonwebdriver are:
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()
The search input box on the Baidu homepage is a case. Enter appium in the search input box to explain the positioning of the elements of the python webdriver. See the source code screenshot of the search input box on the Baidu homepage:
<input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"> |
The id/name/classname/xpath/css selector positioning method is:
# Positioning by id
driver.find_element_by_id('kw').send_keys('appium')
# Locate by name
driver.find_element_by_name('wd').send_keys('appium')
# Locate by class name
driver.find_element_by_class_name('s_ipt').send_keys('appium')
# Locate via xpath
driver.find_element_by_xpath(".//*[@id='kw']").send_keys('appium')
# Positioning via css selector
driver.find_element_by_css_selector('#kw').send_keys('appium')
The method of link text/partiallink to locate the "news" on the Baidu homepage is:
#Locate via link text
driver.find_element_by_link_text(u'新闻').click()
# Positioning via partial link text
driver.find_element_by_partial_link_text(u'闻').click()
2.2 Positioning a group of elements
In certain operations of the browser, sometimes it is necessary to locate a group of objects, such as dragging a box, etc. Webdriver provides a method find_elements to locate a group of elements.
The method of locating a group of objects is the same as the method of locating a single object, see below:
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()
The scenes used to locate a group of objects are generally:
1. Batch operation
2. The other is to select a group of objects and locate specific objects in this group of objects
2.3 Browser operation
Browser maximized |
Driver.maximize_window() |
Set the height and width of the browser |
Driver.set_window_size() |
Control the browser forward |
Driver.forward() |
Control browser back |
Driver.back() |
Browser refresh |
Driver.refersh() |
Get the title of the browser |
Driver.title |
Get the url of the current webpage |
Driver.current_url |
Get the source code of the current page |
Driver.page_source |
Get the name of the executing browser |
Driver.name |
The code example above is as follows:
# coding:utf-8
from selenium import webdriver
from time import sleep
driver=webdriver.Firefox() # If you choose Python3.0, you need to add a driver
# Set the browser height and width
driver.set_window_size(500,600)
# Browser maximize
driver.maximize_window()
driver.implicitly_wait(30)
# Visit Baidu
driver.get('http://www.baidu.com')
# Refresh
driver.refresh()
# Visit bing
driver.get('http://www.bing.com')
# Back
driver.back()
# go ahead
driver.forward()
# Get the title of the browser
print driver.title
# Get the URL of the browser
print driver.current_url
# Get the source code of the current page of the browser
print driver.page_source
# Get the currently executing browser
print driver.name
driver.quit()
2.4 Common methods of WebElement interface
Returns the size of the element |
Driver.size() |
Get the text of the element |
Driver.text |
Get attribute value |
Driver.get_attribute() |
Set whether the changed element is visible |
Driver.is_displayed |
Whether the check box or radio box is checked |
Driver.is_selected() |
Store whether the input edit box is edited |
Driver.is_enabled() |
Empty operation |
Driver.clear() |
Click action |
Driver.click() |
Input operation |
Driver.send_keys() |
****************************** The code example above is as follows: ***************** ************************************************** ********************
#coding:utf-8
from seleniumimport webdriver
from timeimport sleep
driver=webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('http://www.baidu.com')
login=driver.find_element_by_id('su')
# Get element size
print login.size
# Get properties
printlogin.get_attribute('type')
# Whether the element is visible
print u'is it visible:', login.is_displayed()
# Whether the element can be checked
print u'can be checked:', login.is_selected()
# Whether the element is editable
print u'is editable:', login.is_enabled()
driver.quit()
2.5 Baidu login example
You have logged in and logged out of Baidu on the Baidu homepage to complete the positioning of the demo elements and the use of the api part. See the following code:
************************************Code part ************* ************************************************** ***************
# coding:utf-8
from seleniumimport webdriver
from timeimport sleep
driver=webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('http://www.baidu.com')
# Click the login button
driver.find_element_by_link_text(u'登录').click()
# enter account
driver.find_element_by_id('TANGRAM__PSP_8__userName').send_keys('baidu account')
# enter password
driver.find_element_by_id('TANGRAM__PSP_8__password').send_keys('account password')
# Cancel automatic login
driver.find_element_by_id('TANGRAM__PSP_8__memberPass').click()
# Click the login button
driver.find_element_by_id('TANGRAM__PSP_8__submit').click()
# Get nickname
niCheng=driver.find_element_by_xpath(".//*[@id='s_username_top']/span")
text=niCheng.text
niCheng.click()
# Click the exit button
driver.find_element_by_xpath(".//*[@id='s_user_name_menu']/div/a[5]").click()
# Click the OK button
driver.find_element_by_xpath(".//*[@id='tip_con_wrap']/div[3]/a[3]").click()
sleep(2)
assert text in u'Madu Chongshi'
driver.quit()