这次练手的目标:
主要元素定位技术:
使用xpath定位,绝对定位://*[@id=”sciout”]/tbody/tr[2]/td[2]/div/div[3]/span[1]。
使用try …. except 异常捕获,抛出异常。
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from _ast import Pass
calc = webdriver.Chrome()
# Launch website
calc.get("http://www.calculator.net/")
#Maximize the brower
calc.maximize_window()
# The initialization value
one = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[3]/span[1]')
two = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[3]/span[2]')
three = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[3]/span[3]')
add = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[1]/span[4]')
sum1 = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[5]/span[4]')
MR = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[4]/span[5]')
MC = calc.find_element_by_xpath('//*[@id="sciout"]/tbody/tr[2]/td[2]/div/div[5]/span[5]')
resul = calc.find_element_by_id("sciOutPut")
def result(es, res, casename):
if es == res:
print "PASS"
else:
print "FAIL"
calc.get_screenshot_as_file("F:\calc\\%s.png" %casename)
print "F:\calc\\%s.png" %casename
# test main
casename = "casename1"
try:
one.click()
add.click()
two.click()
sum1.click()
res = resul.text
time.sleep(2)
result(3, res, casename)
MR.click()
time.sleep(5)
calc.quit()
except Exception, e:
print e