# coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC driver=webdriver.Firefox()
class Find_Element():
def __inti__(self,driver):
self.driver=driver
def is_exist(self,locator,timeout=30,frequency=0.5):
"""
locator 参数是定位方式,如("id", "kw"),把两个参数合并为一个
*号是把两个参数分开传值
使用方法:
locator = ("id", "kw")
driver.find_element(*locator)
:param locator:
:param timeout:
:param frequency:
:return:
"""
try:
element=WebDriverWait(driver,timeout,frequency).until(lambda driver:driver.find_element(*locator))
return element except:
return False def is_exist2(self,by,value,timeout=30,frequency=0.5):
"""
locator 参数是定位方式,如("id", "kw"),把两个参数合并为一个
*号是把两个参数分开传值
使用方法:
locator = ("id", "kw")
driver.find_element(*locator)
:param locator:
:param timeout:
:param frequency:
:return:
"""
try:
element=WebDriverWait(driver,timeout,frequency).until(lambda driver:driver.find_element(by,value))
return element except:
return False def is_text_in_element(self,locator,text,timeout=30,frequency=0.5):
"""
locator 参数是定位方式,如("id", "kw"),把两个参数合并为一个
*号是把两个参数分开传值
使用方法:
locator = ("id", "kw")
driver.find_element(*locator)
:param locator:
:param timeout:
:param frequency:
:return:
"""
try:
element=WebDriverWait(driver,timeout,frequency).until(EC.text_to_be_present_in_element(locator,text))
return True except:
return False def send_keys(self,text,locate):
self.driver.find_element(*locate).send_keys(text) if __name__=="__main__":
driver=webdriver.Firefox()
driver.get("https://www.baidu.com")
base_driver=Find_Element(driver) #实例化
input_loc=("id","kw")
base_driver.send_keys(input_loc,"致橡树的你")
driver.quit()
 
from selenium.webdriver.common.by import By

driver.find_element("id", "kw")
driver.find_element("xpath", "xxx")
driver.find_element("link text", "xxx")
driver.find_element("partial link text", "xxx")
driver.find_element("name", "xxx")
driver.find_element("tag name", "xxx")
driver.find_element("class name", "xxx")
driver.find_element("css selector", "xxx")

05-23 09:37