标签:xpath try range baidu com 修改 href input 不能
接着上篇随笔 selenium-配置文件定位元素 ,进行了配置文件设置后,将配置文件运用到定位元素中
思路:拿到定位的 key 和 value 后,对 webdrvier 中定位进行封装,使可以直接运用
主要的8种定位方法:
# 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‘)
标签:xpath try range baidu com 修改 href input 不能
原文地址:https://www.cnblogs.com/tynam/p/10351860.html