zoukankan      html  css  js  c++  java
  • Selenium3+webdriver学习笔记4(css方式元素定位)

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-

    from selenium import webdriver

    import time,os

    # about:addons 火狐浏览器安装组件,访问的地址

    # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
    #id
    keys="selenium自动化"
    url="https://www.baidu.com/"
    # url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/select01.html"
    driver=webdriver.Firefox()

    driver.get(url)

    #id class 属性定位 #表示id .表示class 使用标签属性,表示为标签名
    driver.find_element_by_css_selector("#kw").send_keys(keys)

    driver.find_element_by_css_selector(".s_ipt").send_keys(keys)

    #name属性定位
    driver.find_element_by_css_selector("[name='wd']").send_keys(keys)

    #其他属性定位
    driver.find_element_by_css_selector("[autocomplete='off']").send_keys(keys)


    # 标签与属性的组合来定位 id name class 其他属性
    driver.find_element_by_css_selector("input#kw").send_keys(keys)

    driver.find_element_by_css_selector("input[name='wd']").send_keys(keys)

    driver.find_element_by_css_selector("input.s_ipt").send_keys(keys)

    driver.find_element_by_css_selector("input[autocomplete='off']").send_keys(keys)


    # <form name="f" id="form" action="/s" class="fm" onsubmit="javascript:F.call('ps/sug','pssubmit');">
    # <span id="s_kw_wrap" class="bg s_ipt_wr quickdelete-wrap">
    # <span class="soutu-btn"></span><input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">

    #用>表示层级关系 id name class
    driver.find_element_by_css_selector("form#form>span>input").send_keys(keys)
    driver.find_element_by_css_selector("form[name='f']>span>input").send_keys(keys)
    driver.find_element_by_css_selector("form.fm>span>input").send_keys(keys)

    #多属性组合
    driver.find_element_by_css_selector("input[id='kw'][name='wd']").send_keys(keys)


    #select01.html文件
    # 下拉框选择形式
    # <select id="status" class="form-control valid" onchange="" name="status">
    # <option value=""></option>
    # <option value="0">未审核</option>
    # <option value="1">初审通过</option>
    # <option value="2">复审通过</option>
    # <option value="3">审核不通过</option>
    # </select>

    # 定位子元素 nth-child(n)
    driver.find_element_by_css_selector("select#status>option:nth-child(4)").click()

    time.sleep(3)
    driver.quit()


  • 相关阅读:
    CentOS 7 设置iptables防火墙开放proftpd端口
    Android手机修改Hosts的方法
    Wireshark提示没有一个可以抓包的接口
    while read line无法循环read文件
    Nagios新添加的hosts和services有时显示,有时不显示问题解决
    Nginx配置http强制跳转到https
    xargs rm -rf 与 -exec rm
    解决vim不能使用方向键和退格键问题
    NRPE: Unable to read output 问题处理总结
    MySQL 5.6 解决InnoDB: Error: Table "mysql"."innodb_table_stats" not found.问题
  • 原文地址:https://www.cnblogs.com/NiceTime/p/10061758.html
Copyright © 2011-2022 走看看