checkbox复选框
from selenium import
webdriver
from time import
sleep
fromselenium.webdriver.support.selectimportSelect
driver=webdriver.Chrome()
url=’file://C:\checkbox
and radiobox.htm’
driver.get(url)
driver.maximize_window()
sleep(3)
checkboxs_loc=’//input[@type=”checkbox”]’ #//input[@name=”r1”]
checkboxs=driver.find_elements_by_xpath(checkboxs_loc)
i=0
for checkbox in
checkboxs:
# if checkbox in
(checkboxs[0],checkboxs[2]):
i=i+1
checkbox.click()
print(‘第{}复选框被选中啦’.format(i))
driver.save_screenshot(‘D:\Python37\po.png’)
#checkbox.is_selected()
if
checkboxs[2].is_selected():# if checkboxs[2].is_selected()==True:
checkboxs[2].click()
print(‘第三次反选成功’)
else:
print(‘第三次’)
driver.save_screenshot(‘D:\Python37\po1.png’)
sleep(2)
driver.quit()
下拉框
导包
visibility_of_element_located
visibility_of_element_located = 等待所定位到的元素是可见的
frame_to_be_available_and_switch_to_it = frame可用(你找到frame了)并且切入进去
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
dr = webdriver.Chrome()
dr.implicitly_wait(10)
dr.maximize_window()
dr.get('
https://www.juhe.cn/')
login_window_frame_loc = '//iframe[@id=”layui-layer-iframe1”]'
login_window_frame = dr.find_element_by_xpath(login_window_frame_loc)
WebDriverWait(dr, 20).until(EC.frame_to_be_available_and_switch_to_it(login_window_frame))
telephone_loc = '//input[@id=”mobilephone”]'
telephone = dr.find_element_by_xpath(telephone_loc)
telephone.send_keys(13812345678)
sleep(2)
dr.quit()
智能等待
from selenium import webdriver
#driver=webdriver.Chrome()
##隐式等待
#__driver.implicitly_wait(10)
#driver.maximize_window()
#driver.get(‘https://www.juhe.cn/‘)
##__智能等待,显式等待
#__from selenium.webdriver.support.wait import WebDriverWait
###from selenium.webdriver.support import expectedconditions as EC等同于下面两个
#from selenium.webdriver.support.expectedconditions import __ visibility_of_element_located
#from selenium.webdriver.support.expected_conditions import __frame_to_be_available_and_switch_to_it
##WebDriverWait()有两个参数:1、你目前所使用的driver;2、智能等待的设置时间
##.until(条件句)配合WebDriverWait的使用
#弹出的窗口_frame_loc=’//iframe[@id=”layui-layer-iframe1”]’
#弹出的窗口_frame=driver.find_element_by_xpath(弹出的窗口_frame_loc)
##frame_to_be_available_and_switch_to_it()这个条件方法,参数填写你要等待和切入的frame元素
#WebDriverWait(driver,20).until(frame_to_be_available_and_switch_to_it(弹出的窗口___frame))
##visibilityofelementlocated()这个条件方法,参数填写你要智能等待的元素的“元组类型”_的定位器表达式,
##如果在设置的时间内,等待到元素,则返回元素,反之,Python会抛出“等待超时”__这个异常
#_wait=WebDriverWait(driver,20)
#from selenium.webdriver.common.by import By
##定义元组形式的定位器,如果使用xpath表达元素,就写By.XPATH
#手机号码loc=(By.XPATH,’//input[@id=”mobilephone”]’)
_##直接在visibility_of_element_located()方法后面放入“元组形态”的定位器表达式
##等待成功,则返回元素
#手机号码=wait.until(visibilityof_element_located(手机号码_loc))
#print(‘元素长这样:{}’.format(手机号码))
#手机号码.send_keys(‘13812345678’)
#from time import sleep
#sleep(2)
#
#driver.quit()