一:说明
1.需要引入OS
from selenium import webdriver import os
driver = webdriver.Firefox() file_path = 'file:///' + os.path.abspath('E:/tools/evel_locate.html') driver.get(file_path)
os 模块为python 语言标准库中的os 模块包含普遍的操作系统功能。主要用于操作本地目录文件。
path.abspath()方法用于获取当前路径下的文件。另外脚本中还使用到for 循环,对inputs 获取的一组元素
进行循环,在python 语言中循环变量(input)可以不用事先声明直接使用。
inputs= driver.find_elements_by_tag_name('input') #查找input属性的元素 for input in inputs: if input.get_attribute('type')=='checakbox':input.click() #input属性的元素都勾选上print len(driver.find_elements_by_css_selector('input[type=checkbox]'))
driver.find_elements_by_css_selector('input[type=checkbox]').pop(2).click()二:层级定位
driver.find_element_by_link_text('Link1').clik() nume=driver.find_element_by_id('dropdown1').find_element_by_link_text('Another action') #在父亲元件下找到Action子元素 ActionChains(driver).move_to_element(nume).perform()