• selenium-webdriver的二次封装(十)


    接着上篇随笔 selenium-配置文件定位元素 ,进行了配置文件设置后,将配置文件运用到定位元素中

    思路:拿到定位的 key 和 value 后,对 webdrvier 中定位进行封装,使可以直接运用

    主要的8种定位方法:  

    • find_element_by_id   
    • find_element_by_name
    • find_element_by_xpath
    • find_element_by_link_text
    • find_element_by_partial_link_text
    • find_element_by_tag_name
    • find_element_by_class_name
    • find_element_by_css_selector
    # conding=utf-8
    
    from selenium import webdriver
    from auto_learning.config import ReadIni #导入读取的配置文件的py文件,如果不能导入检查是否是包(__init__.py文件存在 )文件
    
    class FindElement(object):
        def __init__(self,driver):
            self.driver = driver
    
        def get_Element(self,pageElement,key):
            # 实例化ReadIni
            file_name = '../config/BaiduElement.ini'
            read_ini = ReadIni.ReadIni(file_name)
            key_value = read_ini.get_value(pageElement,key)
            key = key_value.split(':')[0]
            value = key_value.split(':')[1]
    
            try:
                if key == 'id':
                    return self.driver.find_element_by_id(value)
                elif key == 'class':
                    return self.driver.find_element_by_class_name(value)
                elif key == 'name':
                    return self.driver.find_element_by_name(value)
                elif key == 'tag':
                    return self.driver.find_element_by_tag_name(value)
                elif key == 'linkText':
                    return self.driver.find_element_by_link_text(value)
                elif key == 'cssSelector':
                    return self.driver.find_element_by_css_selector(value)
                elif key == 'xpath':
                    return self.driver.find_element_by_xpath(value)
    
            except:
                return None

    由于浏览器有好多种,所以也需要重新定义浏览器的启动

    对构造函数进行修改

        def __init__(self,driverName):
            try:
                if driverName == 'chrome':
                    self.driver = webdriver.Chrome()
                elif driverName == 'dege':
                    self.driver = webdriver.Edge()
                elif driverName == 'firefox':
                    self.driver = webdriver.Firefox()
                elif driverName == 'safari':
                    self.driver = webdriver.Safari()
    
            except:
                error_message = "find not " + driverName
                print(error_message)
    
            self.driver.maximize_window()
            url = 'https://www.baidu.com'
            self.driver.get(url)

    在项目中如果用单一浏览器可能会造成堵塞

    所以就需要多个浏览器同时跑脚本,不至于一个浏览器的压力太大

    # conding=utf-8
    
    from selenium import webdriver
    import time
    from auto_learning.config import ReadIni #导入读取的配置文件的py文件,如果不能导入检查是否是包(__init__.py文件存在 )文件
    
    class FindElement(object):
        def __init__(self,i):
            self.driver = self.get_driver(i)
            time.sleep(3)
            url = 'https://www.baidu.com'
            self.driver.get(url)
            self.driver.maximize_window()
    
        def get_driver(self,i):
            if i == 0:
                return webdriver.Chrome()
            elif i == 1:
                return webdriver.Edge()
            elif i == 2:
                return webdriver.Firefox()
            elif i == 3:
                return webdriver.Safari()
    
        def get_Element(self,pageElement,key):
            # 实例化ReadIni
            file_name = '../config/BaiduElement.ini'
            read_ini = ReadIni.ReadIni(file_name)
            key_value = read_ini.get_value(pageElement,key)
            key = key_value.split(':')[0]
            value = key_value.split(':')[1]
    
            try:
                if key == 'id':
                    return self.driver.find_element_by_id(value)
                elif key == 'class':
                    return self.driver.find_element_by_class_name(value)
                elif key == 'name':
                    return self.driver.find_element_by_name(value)
                elif key == 'tag':
                    return self.driver.find_element_by_tag_name(value)
                elif key == 'linkText':
                    return self.driver.find_element_by_link_text(value)
                elif key == 'cssSelector':
                    return self.driver.find_element_by_css_selector(value)
                elif key == 'xpath':
                    return self.driver.find_element_by_xpath(value)
    
            except:
                return None
    
    
    if __name__ == '__main__':
        for i in range(3):
            findElement = FindElement(i)
            input_text = findElement.get_Element('BaiduHomeElement','baidu_search_input')
  • 相关阅读:
    HDU3746 Cyclic Nacklace KMP
    KMP的小结
    POJ1916 Period KMP
    POJ 2406 Power Strings 简单KMP模板 strcmp
    HDU 2157 How many ways?? (邻接矩阵快速幂)
    (VIJOS) VOJ 1067 Warcraft III 守望者的烦恼 矩阵快速幂
    puppet之自定义fact(转载)
    Shell标准输出、标准错误 >/dev/null 2>&1
    如何在linux中用命令产生一个范围内的随机数?
    SNMP协议
  • 原文地址:https://www.cnblogs.com/tynam/p/10351860.html
Copyright © 2020-2023  润新知