selenium-webdriver的二次封裝(十)

Tynam.Yang發表於2019-02-04

接著上篇隨筆 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')

 

相關文章