Web自动化

 1 # -*- coding:utf-8 -*-
 2 '''
 3 Created on Oct 17, 2018
 4 
 5 @author: SaShuangYiBing
 6 
 7 Comment: 
 8 '''
 9 from selenium import webdriver
10 driver = webdriver.Firefox()
11 
12 class EnterPage(object):
13     def __init__(self,input_url,element_by_xpath,target_url):
14         self.target_url = target_url
15         self.input_url = driver.get(input_url)
16         self.element_by_xpath = element_by_xpath
17     
18     def locate_element(self):
19         try:
20             driver.find_element_by_xpath(self.element_by_xpath).click()
21             
22         except Exception as e:
23             print (e)
24             
25     def check_result(self):
26         try:
27             cur_url = driver.current_url
28             if cur_url == self.target_url:
29                 print ("The page is loaded successfully")
30             else:
31                 print ("Page load failed")
32                 
33         except Exception as e:
34             print (e)
35             
36 if __name__ == "__main__":
37     ss = EnterPage('https://www.csdn.net/','/html/body/div[2]/nav/div/div/ul/li[8]/a','https://www.csdn.net/nav/db')
38     ss.locate_element()
39     ss.check_result()

打开一个主页,然后点击一个选项并通过链接去判断所要点击的链接是否正常打开,把主页、判断的元素、及要检查的目标网址做为实例参数进行传递,增强程序的灵活性

posted @ 2018-10-17 11:12  iSZ  阅读(278)  评论(0编辑  收藏  举报