zoukankan      html  css  js  c++  java
  • python slenium 中CSS定位

    以百度为例

    一、通过id、class定位

    1、#id:python:driver.find_element_by_css_selector('input#kw')

    2、.class:python:driver.find_element_by_css_selector('input.s_ipt')

    注:class='bg s_ipt_wr quickdelete-wrap',类似这种叫复合class,由多个类选择器组成,定位的写法则是:.bg.s_ipt_wr.quickdelete-wrap,所有空格用.(点)代替

    二、其他属性

    使用其他属性定位,如name,type。。。

    python:driver.find_element_by_css_selector('input[name="wd"]')

    如果属性没有值,也可以使用

    python:driver.find_element_by_css_selector('input[name]')

    三、模糊匹配,定位百度输入框

    匹配属性值由多个空格隔开,匹配其中一个值:input[class~='bg']  # 百度搜索按钮:class='bg s_btn'
    匹配属性值为 'bg' 开头的方法:python:driver.find_element_by_css_selector("input[class^='bg']")
    匹配属性值 ‘ipt’ 结尾的方法:python:driver.find_element_by_css_selector("input[class$='ipt']")
    匹配属性值中间有 '_i' 的元素:python:driver.find_element_by_css_selector("input[class*='_i']")

    四、组合属性定位

    1、input#id.class[name='wd']
    2、多个属性组合input[name][autocomplete]

    五、层级定位

    1、子元素定位(以 > 分隔)

    form>span#id>input[name]

    2、后代元素定位(以空格分隔)

    span input :定位span下所有的input标签,包括span下的所有层次的input,注意不是span下一层的input,是所有

    3、元素层级css还支持三个方法,分别是first-child、last-child、nth-child(n)
    (1)first-child:第一个后代元素
    (2)last-child:最后一个后代元素
    (3)nth-child(N):指定第N个后代元素
    下面举例:
    (1)input:first-child   定位所有层次第一个为input的元素,注意是第一个元素为input标签的
    (2)span input:first-child    定位span标签下,第一个为input标签的元素
    (3)span :last-child   定位span标签下,最后一个元素
    (4)span input:last-child    定位span标签下,最后一个为input标签的元素
    (5)span :nth-child(2)  定位span标签下,第二个元素
    (6)form.fm>:nth-child(2)  定位form标签,class等于fm下的第二个元素

  • 相关阅读:
    python目录操作【os和os.path】
    Zabbix4.0 zabbix 快速监控主机
    Zabbix 4.0 钉钉报警
    MySql:sql99语法的连接查询
    bat脚本中存在多条指令,但只执行到某条指令不继续向下执行的一种解决方法
    基类与接口类中的虚析构函数(virtual destructor)
    TortoiseGit使用指南;
    Rust编译问题Blocking waiting for file lock on package cache
    win10安装visual C++ 6.0,在最后显示安装程序正在更新您的系统,然后就无响应
    从实现装饰者模式中思考C++指针和引用的选择
  • 原文地址:https://www.cnblogs.com/gxfaxe/p/10276322.html
Copyright © 2011-2022 走看看