Web自动化

 # -*- coding:utf-8 -*-
'''
Created on Oct 17, 2018 @author: SaShuangYiBing Comment:
'''
from selenium import webdriver
driver = webdriver.Firefox() class EnterPage(object):
def __init__(self,input_url,element_by_xpath,target_url):
self.target_url = target_url
self.input_url = driver.get(input_url)
self.element_by_xpath = element_by_xpath def locate_element(self):
try:
driver.find_element_by_xpath(self.element_by_xpath).click() except Exception as e:
print (e) def check_result(self):
try:
cur_url = driver.current_url
if cur_url == self.target_url:
print ("The page is loaded successfully")
else:
print ("Page load failed") except Exception as e:
print (e) if __name__ == "__main__":
ss = EnterPage('https://www.csdn.net/','/html/body/div[2]/nav/div/div/ul/li[8]/a','https://www.csdn.net/nav/db')
ss.locate_element()
ss.check_result()

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

上一篇:Mysql之1451 - Cannot delete or update a parent row: a foreign key constraint fails...解决办法记录


下一篇:Apache Lucene(全文检索引擎)—搜索