(1)find_element_by_id()
(2)find_element_by_name()
(3) find_element_by_class_name()
(4)find_element_by_xpath()
有两种形式
一种是绝对路径定位。这种定位方式是利用html标签名的层级关系来定位元素的绝对路径,一般从<html>标签开始依次往下进行查找。如百度搜索框的绝对路径xpath定位可以是这样的:find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input")
还有一种是利用元素属性来进行xpath定位,搜索框还可以利用id和name属性去定位:
通过属性定位元素
find_element_by_xpath("//标签名[@属性='属性值']")
find_element_by_xpath("//input[@id='kw']")
find_element_by_xpath("//*[@name='wd']")
find_element_by_xpath("//input[@maxlength='255']")
find_element_by_xpath("//form[@id='form']/span/input")
//p | //button #选择所有的p和button,等价于css_selector里的 p, button
//input[@id='kw' and @class='su'] #选择id=kw 并且 class="su"的input元素
//div/* div下面的所有的元素
//div//p 先在整个文档里查找div,再在div里查找p节点(只要在内部,不限定是否紧跟) ;等价于 css_selector里的('div p')
//div/p p是div的直接子节点; 等价于 css_selector里的('div > p')
//*[@style] 查找所有包含style的所有元素,所有的属性要加@; 等价于 css_selector里的('*[style]')
//p[@spec='len'] 必须要加引号;等价于 css_selector里的("p[spec='len']")
//p[@id='kw']
//div/p[2] 选择div下的第二个p节点 ;等价于css_selector里的div>p:nth-of-type(2) 符合p类型的第二个节点
//div/*[2] 选择div下第二个元素
//div/p[position()=2] position()=2 指定第二个位置; 等价于上面的 //div/p[2]
position()>=2 位置大于等于2
position()<2 位置小于2
position()!=2 位置不等于2
//div/p[last()] 选择div下的倒数第一个p节点; last()倒数第一个
//div/p[last()-1] 选择div下的倒数第二个p节点;
//div/p[position()=last()] 倒数第一个
//div/p[position()=last()-1] 倒数第二个
//div/p[position()>=last()-2] 倒数第一个,第二个,第三个
(5)find_element_by_link_text() / find_element_by_partial_link_text()
这两种定位方式是专门用于定位超链接的,也就是对应html页面中的<a>标签,括号里传的值就是a标签中的超链接文字,两者的区别在于一个是完整的超链接文字,一个是可以只写部分超链接文字。
(6)find_element_by_tag_name ()
(7)find_element_by_css_selector()
find_element_by_css_selector("#kw")
find_element_by_css_selector(".s_ipt")
find_element_by_css_selector("input")
find_element_by_css_selector("[name='wd']")
find_element_by_css_selector("[maxlength='255']")
find_element_by_css_selector("span>input")
find_element_by_css_selector("input#kw")
find_element_by_css_selector("input.s_ipt")
find_element_by_css_selector("input[name='wd']")
find_element_by_css_selector("span>input.s_ipt")
(8)By
注意:使用By定位方式,需先导入By类。
from selenium.webdriver.common.by import By
find_element(By.ID,"kw")
find_element(By.NAME,"wd")
find_element(By.CLASS_NAME,"s_ipt")
find_element(By.TAG_NAME,"input")
find_element(By.LINK_TEXT,u"新闻")
find_element(By.PARTIAL_LINK_TEXT,u"新")
find_element(By.XPATH,"//*[@class='bg s_btn']")
find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")
(9)elements复数定位
在上面的例举的八中基本定位方式种,都有对应的复数形式,分别是下面这些:
这些复数定位方式每次取到的都是具有相同类型属性的一组元素,所以返回的是一个list队列,
id复数定位find_elements_by_id()
name复数定位find_elements_by_name()
class复数定位find_elements_by_class_name()
tag复数定位find_elements_by_tag_name()
link复数定位find_elements_by_link_text()
partial_link复数定位find_elements_by_partial_link_text()
xpath复数定位find_elements_by_xpath()
css复数定位find_elements_by_css_selector()
(10)JS的定位方式
id定位:document.getElementById()
name定位:document.getElementsByName()
tag定位:document.getElementsByTagName()
class定位:document.getElementsByClassName()
css定位:document.querySelectorAll()
search_js = "document.getElementsByName('wd')[0].value='selenium';"
search_js2 = "document.querySelectorAll('.s_ipt')[0].value='selenium';"
button_js = "document.getElementById('su').click();"
button_js2 = "document.getElementsByClassName('s_btn')[0].click()"
driver.execute_script(search_js2)
driver.execute_script(button_js2)