css选择器定位页面元素(注意:css选择器定位元素的速度比xpath快)
1.selenium通过css定位元素
选择器 | 例子 | 描述 |
.class | .intro | 选择class="intro"的所有元素 |
#id | #firstname | 选择id="firstname"的所有元素 |
* | * | 选择所有元素 |
element | p | 选择所有<p>元素 |
element,element | div,p | 选择所有<div>和所有的<p>元素 |
element element | div p | 选择<div>元素内部的所有<p>元素 |
element > element | div > p | 选择父元素为<div>下的所有<p>元素 |
[attribute] | [target] | 选择带有target属性所有元素 |
[attribute=value] | [target=_blank] | 选择target="_blank"的所有元素 |
[attribute~=value] | [title~=flower] | 选择title属性包含单词“flower”的所有元素 |
2.css选择器定位元素—id
3.css选择器定位元素—css
4.css选择器定位元素——通过属性定位元素
4.1通过属性定位
4.2通过层级定位
扫描二维码关注公众号,回复:
1143572 查看本文章
4.3完整路径定位
#-*- encoding:utf-8 -*- from selenium import webdriver import time driver = webdriver.Firefox() driver.get("http://cn.bing.com/") driver.find_element_by_css_selector("html body.zhs.zh-CN table#hp_table tbody tr td#hp_cellCenter.hp_hd div#hp_container div#sbox.sw_sform div.search_controls form#sb_form.sw_box div.b_searchboxForm input#sb_form_q.b_searchbox").send_keys('selenium') driver.find_element_by_css_selector("html body.zhs.zh-CN table#hp_table tbody tr td#hp_cellCenter.hp_hd div#hp_container div#sbox.sw_sform div.search_controls form#sb_form.sw_box div.b_searchboxForm input#sb_form_go.b_searchboxSubmit").click()