selenium 之 css定位

selenium css定位

css概述:

  1. css(cascading style sheets)是一种语言,它用来描述HTML和XML的元素显示样式。

  2. css语言中有css选择器,在selenium中也可以使用这种选择器来进行元素定位。

  3. css定位方式比xpath快,而且css的语法也非常强大。

    定位方法:find_element_by_css_selector()

    css定位方式:1. id选择器 2. class选择器 3. 元素选择器 4. 属性选择器 5. 层级选择器

  4. id选择器

    根据元素id属性来选择

    格式:#id属性值 如:#userA(选择id属性值为userA的所有元素)

    find_element_by_css_selector('#userA').send_keys('hello selenium')

  5. class选择器

    根据元素class属性来选择

    格式:.class属性值 如:.telA(选择class属性值为telA的所有元素)

    find_element_by_css_selector('.telA').send_keys('hello selenium')

  6. 元素选择器

    根据元素标签名来选择

    格式:element 如: input(选择所有input元素)

    find_element_by_css_selector('input').send_keys('hello selenium')

  7. 属性选择器

    根据元素的属性名和值来选择

    格式:attribute=value 如:type='password' (选择所有type属性为password的值)

    find_element_by_css_selector('type="password"').send_keys('hello selenium')

  8. 层级选择器

    根据元素的父子关系来选择

    格式:element>element 如:p>input(返还所有p元素下所有的input元素)

    find_element_by_css_selector('p>input').send_keys('hello selenium')

相关推荐
测试19987 小时前
Selenium 无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
feixing_fx8 小时前
告别枯燥字体:Web 字体加载策略与 font-display 最佳实践
前端·css·前端框架·交互·css3
ly768912 小时前
CSS 从入门到进阶:系统掌握现代网页样式与布局
前端·css
前端技术官13 小时前
一行 CSS 新特性干掉 20 行 JavaScript ?
css·滚动驱动动画·容器查询·:has()·anchor positioning
CIO_Alliance14 小时前
AI深度系列(1)|神经元激活函数与MLP原理:理解神经网络的基础
人工智能·深度学习·神经网络·机器学习·tensorflow·ai+ipaas·企业cio联盟
老郑聊AI业财智造14 小时前
TensorFlow 技术架构与源码分析
人工智能·python·深度学习·架构·tensorflow·软件工程
Beginner x_u15 小时前
Tailwind CSS 速通:有 CSS 基础的实战入门
css·tailwindcss·工程化
WL_arm1 天前
Vibe Coding(氛围编程)入门
javascript·css·人工智能·html5
belldeep2 天前
python:Selenium 4.47 编码新的写法
python·selenium
IT小白杨2 天前
工程视角下的浏览器环境API:资源模型、权限审计与MCP实践
大数据·经验分享·selenium·chrome devtools·安全架构·指纹浏览器