selenium模块---操作浏览器

Posted zihkj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium模块---操作浏览器相关的知识,希望对你有一定的参考价值。

前言:selenium模块是用来做UI自动化,模拟浏览器操作

1、安装:

终端安装:pip install selenium

编译器安装:File->Settings->Project:文件名称->Project lnterpreter

技术图片

 

 

2、导入模块:

from selenium import webdriver

 

3、操作浏览器

from Common.read_ini import ReaDconfigurationFile
from selenium.webdriver.common.action_chains import ActionChains
import time
from PIL import Image
from selenium import webdriver
import time
import os

class FindElement():
    def __init__(self):
        self.read = ReaDconfigurationFile()

        # if bdriver == ‘chrome‘:
        #     self.driver = webdriver.Chrome()
        # if bdriver == ‘firefox‘:
        #     self.driver = webdriver.Firefox()
        # if bdriver ==‘ie‘:
        #     self.driver =  webdriver.Ie()
        # return None
    # 打开浏览器
    def get_bdriwer(self,bdriver):
        if bdriver == chrome:
            self.driver = webdriver.Chrome()
        if bdriver == firefox:
            self.driver = webdriver.Firefox()
        if bdriver ==ie:
            self.driver =  webdriver.Ie()
        return None

    #进入指定的url
    def get_to_url(self,url):
        self.driver.get(url)

    #验证元素是否存在
    def get_element(self,keys):
        data = self.read.obtain_ini_value(keys)
        by = data.split(:)[0]
        key = data.split(":")[1]
        try:
            if by==id:
                return self.driver.find_element_by_id(key)
            elif by==name:
                return self.driver.find_element_by_name(key)
            elif by==class_name:
                return self.driver.find_element_by_class_name(key)
            elif by==link_text:
                return self.driver.find_element_by_link_text(key)
            elif by==partial_link_text:
                return self.driver.find_element_by_partial_link_text(key)
            elif by==xpath:
                return self.driver.find_element_by_xpath(key)
            elif by==css_selector:
                return self.driver.find_element_by_css_selector(key)
        except:
            return None

    #输入事件
    def input_event(self,key,content,):
        return self.get_element(key).send_keys(content)

    # #点击事件
    def click_event(self,keys):
        return self.get_element(keys).click()

    #获取文字信息
    def text_info(self,keys,keyss):
        return self.get_element(keys).get_attribute(keyss)

    #获取浏览器title
    def get_title_text(self):
        return self.driver.title

    #清除输入框内容
    def clear_content(self,keys):
        return self.get_element(keys).clear()

    #获得下来框文本的值
    def get_text(self,keys):
        return self.get_element(keys).text

    # #鼠标移动到某个元素上面
    def mobile_mouse(self,keys):
        Action = ActionChains(self.driver)
        move_mouse = self.get_element(keys)
        return Action.move_to_element(move_mouse)

    #拖拽
    def drag_and_drop(self,keys,keyss):
        Action = ActionChains(self.driver)
        element = self.get_element(keys)
        aims = self.get_element(keyss)
        return Action.drag_and_drop(element,aims).perform()
        #self.Action.click_and_hold(element).move_to_element(aims).perform()

    #浏览器前进操作
    def webdriver_go_ahead(self):
        return self.driver.forward()

    #浏览器后退操作
    def webdriver_back(self):
        return self.driver.back()

    #切换到系统弹框
    #点击确认按钮
    def switch_to_confirm(self):
        return self.driver.switch_to_alert().accept()

    # 切换到系统弹框
    # 点击确认按钮
    def switch_to_cancel(self):
        return self.driver.switch_to_alert().dismiss()

    # 切换到系统弹框
    # 点击输入文本内容
    def switch_to_input(self,content):
        return self.driver.switch_to_alert().send_keys(content)

    #切换到系统弹窗
    #获取文本内容
    def pop_text_info(self):
        return self.driver.switch_to_alert().text

    #关闭浏览器
    def close_driver(self):
        return self.driver.close()

    #截图保存图片
    def screenshot(self,file_name):
        file = file_name
        return self.driver.save_screenshot(file)

    #裁剪验证码图片
    def crop_Image(self,file_name,keys):
        #保存图片
        self.screenshot(file_name)
        #定位到图片位置
        images = self.get_element(keys)
        #定位四个点
        left = images.location[x]  #
        top = images.location[y]  #
        right = images.size[width] + left  #
        height = images.size[height] + top  #
        #打开图片
        im = Image.open(file_name)
        #裁剪图片
        img = im.crop(left,top,right,height)
        #保存图片
        img.save(file_name)

    #获取页面所有的handle
    def extract_handle(self):
        return self.driver.window_handles()

    #进入指定的handle页面
    def get_to_page(self,key):
        return self.driver.switch_to_window(key)

    #等待时间
    def wait_time(self,second):
        return time.sleep(second)


if __name__ == __main__:
    fe = FindElement(chrome)
    fe.get_to_url(
        https://passport.58.com/login/?path=https%3A//wh.58.com/%3Futm_source%3Dmarket%26spm%3Du-2d2yxv86y3v43nkddh1.BDPCPZ_BT&PGTID=0d100000-0009-e0b9-7ab5-9885257cdd77&ClickID=2
        )
    fe.click_event(error_message)
    time.sleep(3)
    fe.input_event(username,123456)

以上是关于selenium模块---操作浏览器的主要内容,如果未能解决你的问题,请参考以下文章

selenium模块

selenium模块

爬虫-selenium模块

selenium模块

selenium自动化模块的应用

selenium模块