selenium 显示等待wait.until 常用封装 及下拉框的选择操作等

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wwait
from selenium.webdriver.support import expected_conditions as EC

def
wait_until(bc,locator,type=1): '''bc=driver,类似locator=(By.ID,'kw'),type{1:visible,2:clickable,3:frame switch}''' wait=wwait(bc,10,0.2)   
  #等待页面元素可见,返回该页面元素
if type==1: return wait.until(EC.visibility_of_element_located(locator))   
  #等待页面元素可点击,返回该元素
elif type==2: return wait.until(EC.element_to_be_clickable(locator))
  #通过定位frame 切换到这个frame
elif type=3: wait.until(EC.frame_to_be_available_and_switch_to_it(locator))

  #切换回最外层 bc.switch_to.default_content()

 #下拉框、单选框、文本下拉框的选择操作

from selenium.webdriver import ActionChains as AC
from selenium import webdriver as wd
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys

#
找到下拉框 select_element=Select(bc.find_element_by_xpath("//select"))
#下拉框选项的多选 select_element.select_by_index(1) select_element.select_by_visible_text('荔枝') select_element.select_by_value('juzi')
#取消选择 select_element.deselect_by_index(1) select_element.deselect_by_visible_text('荔枝') select_element.deselect_by_value('juzi')
#文本下拉框 from selenium.webdriver.common.keys import Keys bc.find_element_by_id('select').send_keys('c') #箭头向下选 bc.find_element_by_id('select').send_keys(Keys.ARROW_DOWN) #回车选中 bc.find_element_by_id('select').send_keys(Keys.ENTER)
#操作单选框 berry_redio=bc.find_element_by_xpath("//input[@value='berryradio']") berry_redio.click() if berry_redio.selected(): watermelonradio=bc.find_element_by_xpath("//input[@value='watermelonradio']") watermelonradio.click()
#复选框 先找到复选框 berry_checkbox=bc.find_element_by_xpath("//input[@value='berry']") berry_checkbox.click() if berry_checkbox.is_selected(): berry_checkbox.click() #取消选择

check_boxlist=bc.find_elements_by_xpath("//input[@name='fruit']") for i in check_boxlist: if not i.is_selected(): i.click()

 

转载于:https://www.cnblogs.com/xiaoxiao075/p/10661145.html

你可能感兴趣的:(selenium 显示等待wait.until 常用封装 及下拉框的选择操作等)