css_selector实例

本文详细解析了CSS选择器的优先级规则,包括如何使用ID提高样式特异性、避免使用!important的重要性以及链接状态LVHA(link-visited-hover-active)的应用等。通过实例帮助读者理解CSS中不同选择器的具体作用范围。
1 * { } 0
3 li:first-line { } 2 (one element, one pseudo-element)
5 ul ol+li { } 3 (three elements)
7 ul ol li.red { } 13 (one class, three elements)
9 style=”” 1000 (one inline styling)
11 div p { } 2 (two HTML selectors)
13 div p.sith { } 12 (two HTML selectors and a class selector)
15 body #darkside .sith p { } 112 (HTML selector, id selector, class selector, HTML selector; 1+100+10+1)

实际经验:

1. 使用LVHA定义链接,即link-visited-hover-active,参考[Link Specificity]
2. 如果不是为了兼容浏览器,最好不要使用 !important
3. 使用ID让特性更具体,比如a.high写成 ul#blog li a.high

CSS工具与资 英文好的去看下

CSS Specificity for Poker Players
CSS specificity calculator
Understanding Specificity Tutorial
Cascade Inheritance: Specificity
CSS 2.1 Selectors Explained
CSS Specificity Bugs in IE
CSS Structure and Rules
Specificity


Related Posts
No related posts
优先级 css Specificity
在Selenium中,`By.CSS_SELECTOR`是一种强大的元素定位方式,可通过CSS选择器来定位网页元素。以下是其具体用法: #### 基本用法 在使用`By.CSS_SELECTOR`时,需要先导入`By`类,然后结合`find_element`或`find_elements`方法进行元素定位。 ```python from selenium import webdriver from selenium.webdriver.common.by import By # 创建WebDriver实例对象,指明使用chrome浏览器驱动 wd = webdriver.Chrome(r'D:\tools-work\chromedriver_win32\chromedriver.exe') # 让浏览器打开指定网址 wd.get('http://cdn1.python3.vip/files/selenium/sample1.html') # 使用By.CSS_SELECTOR定位单个元素 element = wd.find_element(By.CSS_SELECTOR, 'p') print(element.text) # 使用By.CSS_SELECTOR定位多个元素 elements = wd.find_elements(By.CSS_SELECTOR, 'p') for e in elements: print(e.text) ``` #### 常见的CSS选择器示例 - **通过元素标签名定位**:可以直接使用元素的标签名作为选择器。 ```python # 定位所有的<p>元素 elements = wd.find_elements(By.CSS_SELECTOR, 'p') ``` - **通过类名定位**:使用`.`符号加上类名。 ```python # 定位class为animal的元素 elements = wd.find_elements(By.CSS_SELECTOR, '.animal') ``` - **通过ID定位**:使用`#`符号加上ID名。 ```python # 定位id为myId的元素 element = wd.find_element(By.CSS_SELECTOR, '#myId') ``` - **属性选择器**:可以根据元素的属性来定位。 ```python # 定位所有具有href属性的<a>元素 elements = wd.find_elements(By.CSS_SELECTOR, 'a[href]') # 定位href属性值为特定值的<a>元素 element = wd.find_element(By.CSS_SELECTOR, 'a[href="https://example.com"]') ``` - **组合选择器**:可以将多个选择器组合使用。 ```python # 定位class为animal的<div>元素下的所有<p>元素 elements = wd.find_elements(By.CSS_SELECTOR, 'div.animal p') ``` #### 定位class属性中包含某段文字的元素 可以使用属性包含特定值的属性选择器`[attribute*=value]`。 ```python # 定位class属性中包含'PGT_disabled'的元素 elements = wd.find_elements(By.CSS_SELECTOR, "[class*='PGT_disabled']") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值