美文网首页我爱编程
python+selenium自动化的常用小方法

python+selenium自动化的常用小方法

作者: 莫依痕 | 来源:发表于2018-02-02 20:04 被阅读0次

1、循环勾选复选框

  • input标签
CheckBox-input.png
        checkboxs = driver.find_elements_by_xpath(".//*[@type='checkbox']")
        for  i in checkboxs:
            i.click()
  • input标签
非input标签的CheckBox.png
        checkboxs = driver.find_elements_by_tag_name('i')  #这里获取的是标签的名字,如果是其他的,对应修改即可
        for checkbox in checkboxs:
            value = checkbox.get_attribute("class") #获取input标签的class
            if value.find("checklist__checkitem") > -1: #find里的内容获取的是class的部分内容
                checkbox.click()

2、跑自动化时,记得随时杀chromedriver.exe进程

这个方法还是比较好用的,要不然任务管理器查看,会生成一堆的chromedriver.exe进程,电脑会变得很卡

import os
command = 'taskkill /IM chromedriver.exe /F'
os.system(command)
print("我的功能是杀chromedriver.exe进程")

注意:使用时,要导入os,否则会报错:NameError: name 'os' is not defined

相关文章

网友评论

    本文标题:python+selenium自动化的常用小方法

    本文链接:https://www.haomeiwen.com/subject/rhgezxtx.html