Web自动化

2018-10-19 06:17:53来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 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()

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Python学习 day01打卡

下一篇:day01-Python输出