Python (c): Python Automated test positioning

Source: Internet
Author: User

Webdriver provides eight ways to locate elements:
ID, Name, class name, tag name, link text, Partial link text, XPath, CSS selector
The corresponding positioning methods in the Python language are as follows:
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 ()
Use:

Import Sys
Import Urllib
Import Urllib2
From selenium import Webdriver
Import OS
Import WebBrowser
Import Time,unittest
Print Time.ctime () #打印当前时间


#登录
def login (Un,url):
Driver = Webdriver. Ie ()
Driver.implicitly_wait (10)
Driver.get (URL)
Driver.find_element_by_name ("username"). Clear ()
Driver.find_element_by_name ("username"). Send_keys (un)
Driver.find_element_by_name ("password"). Clear ()
Driver.find_element_by_name ("password"). Send_keys ("password")
Driver.find_element_by_name ("Subenter"). Click ()
Time.sleep (3)
# Get assertion information to assert
Username=driver.find_element_by_xpath ("//html/body/table/tbody/tr/td/*/tbody/tr/td/table/tbody/tr/td/b/font"). Text
Print type (username)
Username1=username
Print type (username1)
#print username
Print "%s:welcome to the system"%username1
F=open (R ' d:\\python\\a.txt ', ' AB ')
F.write (Username1.encode (' gb2312 '))
F.write (': Welcome to the system\n ' + ' \ n ')
F.close ()
Driver.get_screenshot_as_file ("d:\\error.jpg") #功能
If Un.encode (' gb2312 ') ==username.encode (' gb2312 '):
Print "Success"
Else
Print "Fail"
#退出网站
def exit (un):
Driver.find_element_by_class_name ("Personset"). Click ()
F=open (R ' d:\\python\\a.txt ', ' AB ')
F.write (Un.encode (' gb2312 '))
F.write (': Exit the system\n ' + ' time.ctime () ' + ' \ n ')
Print Time.ctime () #打印当前时间
F.close ()

#从txt获取用户名
Source=open ("D:\\python\\name.txt", "R") #用户名文件
Values=source.readline () #读取用户名, all user names ReadLines
Source.close ()
Un=values
Url= "http://192.168.1.80:7001/"
#un = "Chenyang"
Login (Values,url)
‘‘‘
Multiple user names Loop login
For UN in values:
Try
Login (Un,url)
Except IOError:
Print "Login failed! "#异常打印
‘‘‘

Python (c): Python Automated test positioning

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.