欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Web自动化

程序员文章站 2022-07-02 14:11:53
打开一个主页,然后点击一个选项并通过链接去判断所要点击的链接是否正常打开,把主页、判断的元素、及要检查的目标网址做为实例参数进行传递,增强程序的灵活性 ......
 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()

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