css selector只匹配最近的一层

html-css014

css selector只匹配最近的一层,第1张

语法:find_element_by_css_selector("css选择器定位策略”) 或者 find_elements_by_css_selector("css选择器定位策略”)

1、css可以通过元素的 id,class,标签 这三个常规属性直接定位到

tips:若用id定位,则用 #。若用class定位,则用 .

下面是百度搜索框的HTML代码:

通过css selector定位有如下三种常规方式:

  find_element_by_selector("#kw") (#表示通过id定位)

find_element_by_selector(".s_ipt") (. 表示通过class定位)

find_element_by_selector("标签名“”) 其实单纯通过标签名来定位元素,是有很大局限性的,因为一个页面中,非常大可能的

存在标签名的重复,因此无法很精确的定位

$str = '<link rel="dns-prefetch" href="//imgcache.a.com">

<link  type="text/css" href="

<link  href="index1.css" rel="stylesheet" type="text/css">

<link  type="text/css" rel="stylesheet" href="/index2.css"  >' //如果是第三和第四行加上,那第二行的"

应该是多打了一个"x"

$preg_csslink = '/<link[\w\W]*?type="text\/css"[\w\W]*?>/i'

preg_match_all($preg_csslink, $str, $arr_csslink)

$csslink_source = $arr_csslink[0]

foreach ($arr_csslink[0] as &$val) {

$preg_href = '/href="(.*?)\.css"/i'

preg_match($preg_href, $val, $arr_href)

$href = $arr_href[1]

if (strpos($href, '

) !== 0) {

if (strpos($href, '/') === 0) {

$val = str_replace($href, '

$href, $val)

} else {

$val = str_replace($href, '

$href, $val)

}

}

}

$new_str = str_replace($csslink_source, $arr_csslink[0], $str)

:first-child /* 匹配第一个元素 */

举例:(parent下的第一个子元素字体变红色)

.parent > :first-child {color:red} <div class="parent">

    <p>子元素1</p>

    <div>子元素2</div>

    <span>子元素3</span>

</div>