HTML简介
<html> #<></>标签对,html为标签名
<body>
</body> #层级
</html>
<div id="id1" class="class1"> #标签属性:id,class
<input name="name1">
<a href="http://www.google.com">google</a> #a为超链接标签,href为超链接,google为超链接文本
find_element_by定位
find_element_by_id("id1") #id定位
find_element_by_name("name1") #name定位
find_element_by_class_name("class1") #class定位
find_element_by_tag_name("tag1") #tag标签定位
find_element_by_link_text("linktext1") #link链接文本定位
find_element_by_partial_link_text("partialLinktext1") #link链接部分文本定位
XPath定位:find_element_by_xpath("")
find_element_by_xpath("/html/div/div[2]/from/span/input") #/绝对路径
find_element_by_xpath("//input[@id="id1"]") #//相对路径,标签名[@属性=属性值]
find_element_by_xpath("//*[@class='class1']") #*通配符
find_element_by_xpath("//span[@type='type1']//input[@name='name1']") #层级关系
find_element_by_xpath("//input[@id='id1' and @class='class1']/input") #and逻辑操作
CSS定位:find_element_by_css_selector("")
find_element_by_css_selector("input.class1") #标签.属性名,标签不需要修饰符
find_element_by_css_selector("#id1") ##id属性名
find_element_by_css_selector("[name='name1']") #[属性=属性名]
find_element_by_css_selector("span.class1>input#id1") #>父子
XPath&CSS对比
两种方法均可通过火狐浏览器插件FireBug自动获得
定位方式 | XPath | CSS |
---|---|---|
tag | //div | div |
id | //div[@id='id1'] | div#id1 |
class | //div[@class='class1'] | div.class1 |
property | //div[@title='title1'] | div[title=title1] |
hierarchy | //div[@id='id1']/* | div#id>* |
By定位
实质上与上面的一样,统一了形式。
#from selenium.webdriver.commom.by import By
find_element(By.ID,"id1") #By.类型,方式(大写),其余5种类似
find_element(By.XPATH,"//*[@class='class1']")
find_element(By.CSS_SELECTOR,"span.class1")