对webdriver的简单封装

from selenium import webdriver

class Browser():
    def __init__(self, webdriver):
        self.driver = webdriver

    #打开浏览器
    def get(self, url):
        self.driver.get(url)

    def quit(self):
        self.driver.quit()

    #根据id查找元素
    def findId(self, id):
        find = self.driver.find_element_by_id(id)
        return  find

    #根据名称查找元素
    def findName(self, name):
        find = self.driver.find_element_by_name(name)
        return find

    #根据类名查找元素。
    def findClassName(self, className):
        find = self.driver.find_element_by_class_name(className)
        return find

    #根据Tag Name查找元素
    def findTagName(self, tagName):
        find = self.driver.find_element_by_tag_name(tagName)
        return find

    def findLinkText(self, text):
        find = self.driver.find_element_by_link_text(text)
        return find

    def findPartialLinkText(self, text):
        find = self.driver.find_element_by_partial_link_text(text)
        return find

    def findXPath(self, xpath):
        find = self.driver.find_element_by_xpath(xpath)
        return find

    #根据CSS选择器查找元素。
    def findCSS(self, css):
        find = self.driver.find_element_by_css_selector(css)
        return find


    #多个元素的查找
        # 根据id查找元素
        def findAllId(self, id):
            find = self.driver.find_elements_by_id(id)
            return find

        # 根据名称查找元素
        def findAllName(self, name):
            find = self.driver.find_elements_by_name(name)
            return find

        # 根据类名查找元素。
        def findAllClassName(self, className):
            find = self.driver.find_elements_by_class_name(className)
            return find

        # 根据Tag Name查找元素
        def findAllTagName(self, tagName):
            find = self.driver.find_elements_by_tag_name(tagName)
            return find

        def findAllLinkText(self, text):
            find = self.driver.find_elements_by_link_text(text)
            return find

        def findAllPartialLinkText(self, text):
            find = self.driver.find_elements_by_partial_link_text(text)
            return find

        def findAllXPath(self, xpath):
            find = self.driver.find_elements_by_xpath(xpath)
            return find

        # 根据CSS选择器查找元素。
        def findAllCSS(self, css):
            find = self.driver.find_elements_by_css_selector(css)
            return find


#后面是测试代码
from selenium import webdriver
from browser import Browser
from time import sleep

browser = Browser(webdriver.Firefox())

browser.get("https://www.baidu.com")

browser.findId("kw").send_keys("python selenium")
browser.findId("su").click()

sleep(2)

browser.quit()

 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值