开发者社区> 1032199045570561> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

selenium 封装

简介: 笔记
+关注继续查看

周末无聊 在家封装一个pyselenium。可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的封装去写脚本,这样的,我还是可以更加灵活快捷的去完成我的代码,其实我相信很多的网上都会有大神对这个的封装,那么我也是根据我业余学习的只知识,在加上我自己的摸索去封装的几个方法,其实我也没怎么去大规模的去试下这个是否能够更快的去使用,但是我封装的函数都是我自己能看懂的,可以直接去使用的。

经过简单的封装,让我们在使用的时候更加方便快捷,更加用起来顺手,虽然笔者不曾大规模的使用,但是也是根据我的经验去试着去对这些进行封装

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
 
class pyselenium():
    def __init__(self,brower):#初始化浏览器
        if brower =='firefox' or brower =='Firefox' or brower =='f' or brower =='F':
            deriver=webdriver.Firefox()
        elif brower =='Ie' or brower =='ie' or brower =='i' or brower=='I':
            deriver=webdriver.Ie()
        elif brower =='Chrome' or brower =='chrome' or brower =='Ch' or brower=='ch':
            deriver=webdriver.Chrome()
        elif brower =='PhantomJS' or brower =='phantomjs' or brower =='ph' or brower=='phjs':
            deriver=webdriver.PhantomJS()
        elif brower =='Edge' or brower =='edge' or brower =='Ed' or brower=='ed':
            deriver=webdriver.Edge()
        elif brower =='Opera' or brower =='opera' or brower =='op' or brower=='OP':
            deriver=webdriver.Opera()
        elif brower =='Safari' or brower =='safari' or brower =='sa' or brower=='saf':
            deriver=webdriver.Safari()
        else:
            raise NameError('只能输入firefox,Ie,Chrome,PhantomJS,Edge,Opera,Safari')
        self.driver=deriver
    def element(self,fangfa,dingwei):#定位
        if fangfa=='id':
            element=self.deriver.find_element_by_id(dingwei)
        elif fangfa == "name":
            element = self.driver.find_element_by_name(dingwei)
        elif fangfa == "class":
            element = self.driver.find_element_by_class_name(dingwei)
        elif fangfa == "link_text":
            element = self.driver.find_element_by_link_text(dingwei)
        elif fangfa == "xpath":
            element = self.driver.find_element_by_xpath(dingwei)
        elif fangfa == "tag":
            element = self.driver.find_element_by_tag_name(dingwei)
        elif fangfa == "css":
            element = self.driver.find_element_by_css_selector(dingwei)
        else:
            raise NameError("Please enter the  elements,'id','name','class','link_text','xpath','css','tag'.")
        return element
    def elements(self,fangfa,dingwei):#组定位
        if fangfa=='id':
            element=self.driver.find_elements_by_id(dingwei)
        elif fangfa == "name":
            element = self.driver.find_elements_by_name(dingwei)
        elif fangfa == "class":
            element = self.driver.find_elements_by_class_name(dingwei)
        elif fangfa == "link_text":
            element = self.driver.find_elements_by_link_text(dingwei)
        elif fangfa == "xpath":
            element = self.driver.find_elements_by_xpath(dingwei)
        elif fangfa == "tag":
            element = self.driver.find_elements_by_tag_name(dingwei)
        elif fangfa == "css":
            element = self.driver.find_elements_by_css_selector(dingwei)
        else:
            raise NameError("Please enter the  elements,'id','name','class','link_text','xpath','css','tag'.")
        return elements
    def element_wait(self,fangfa,dingwei,wati=6):#等待
        if fangfa == "id":
            WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.ID, dingwei)))
        elif fangfa == "name":
            WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.NAME, dingwei)))
        elif fangfa == "class":
            WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.CLASS_NAME, dingwei)))
        elif fangfa == "link_text":
            WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.LINK_TEXT, dingwei)))
        elif fangfa == "xpath":
            WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.XPATH, dingwei)))
        elif fangfa == "css":
            WebDriverWait(self.driver,wati,1).until(EC.presence_of_element_located((By.CSS_SELECTOR, dingwei)))
        else:
            raise NameError("Please enter the  elements,'id','name','class','link_text','xpath','css'.")
    def open(self,url):#打开网页
        self.driver.get(url)
    def make_maxwindow(self):#最大化浏览器
        self.driver.maximize_window()
    def set_winsize(self,wide,hight):#设置窗口
        self.driver.set_window_size(wide,hight)
    def send_ke(self,fangfa,dingwei,text):#发送内容
        self.element(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        e1.clear()
        e1.send_keys(text)
    def clea(self,fangfa,dingwei):#清空
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        e1.clear()
    def click(self,fangfa,dingwei):#单击
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        e1.click()
    def right_click(self,fangfa,dingwei):#右击
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        ActionChains(self.deriver).context_click(e1).perform()
    def move_element(self,fangfa,dingwei):#移动到
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        ActionChains(self.deriver).move_element(e1).perform()
    def double_click(self,dingwei,fangfa):#双击
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        ActionChains(self.driver).double_click(e1).perform()
    def  drag_and_drop(self,fangfa1,e1,fangfa2,e2):#从e1到e2
        self.element_wait(fangfa1,e1)
        eme1=self.element(fangfa1,e1)
        self.element_wait(fangfa2,e2)
        eme2=self.element(fangfa2,e2)
        ActionChains(self.deriver).drag_and_drop(eme1,eme2).perform()
    def click_text(self,text):#点击文字
        self.driver.find_element_by_link_text(text).click()
    def close(self):#关闭
        self.driver.close()
    def kill(self):#退出
        self.driver.quit()
    def sublimit(self,fangfa,dingwei):#提交
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        e1.sublimit()
    def f5(self):#刷新
        self.driver.refresh()
    def js(self,sprit):#执行js
        self.driver.execute_script(sprit)
    def get_attribute(self, fangfa,dingwei, attribute):
        e1=self.element(fangfa,dingwei)
        return e1.get_attribute(attribute)
    def get_text(self,fangfa,dingwei):
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        return e1.text
    def get_is_dis(self,fangfa,dingwei):
        self.element_wait(fangfa,dingwei)
        e1=self.element(fangfa,dingwei)
        return e1.is_displayed()
    def get_title(self,fangfa,dingwei):#获取title
        return self.driver.title()
    def get_screen(self,file_path):#截屏
        self.driver.get_screenshot_as_file(file_path)
    def wait(self,fangfa,dingwei):#等待
        self.driver.element_wait(fangfa,dingwei)
    def accpet(self):#允许
        self.driver.switch_to.alert.accept()
    def dismiss_alert(self):
        self.driver.switch_to.alert.dismiss()
    def switch_to_frame(self, fangfa,dingwei):#切换
        self.element_wait(fangfa,dingwei)
        if1=self.element(fangfa,dingwei)
        self.driver._switch_to.frame(if1)
if __name__ == '__main__':
    driver=pyselenium(brower="firefox")
    driver.open('http://www.baidu.com')

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
【selenium】反屏蔽方法
简介:【selenium】反屏蔽方法
147 0
【selenium】下载文件
简介:【selenium】下载文件
33 0
【selenium】下拉滚动条
简介:【selenium】下拉滚动条
102 0
(二)selenium的实际运用
这里我们只会用到很少的selenium语法,我这里就不补充别的用法了,以实战为目的
57 0
(二)selenium的实际运用
这里我们只会用到很少的selenium语法,我这里就不补充别的用法了,以实战为目的
50 0
selenium爬取拉勾
用到的知识点: selenium 显式等待 两个窗口的切换 '''用selenium爬取拉勾''' from lxml import etree from selenium import webdriver from selenium.
903 0
selenium的API
作者:Glen.He 出处:http://www.cnblogs.com/puresoul/  1.1  下载selenium2.0的包 官方download包地址:http://code.
771 0
242
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载