xpth定位
- xpth绝对路径定位,定位div/div/下面的input元素
find_element_by_xpath("/div/div/input")
- 相对路径定位
#定位input标签下面的name元素
find_element_by_xpath("//input[@name="name"]")
#不指定标签可以用*号代替,*号代表所有
find_element_by_xpath("//*[@name="name"]")
#定位div下面的input/tc元素,相对路径定位如果子元素没有没有可标识的属性。可以层级往上找父元素的属性,然后根据父元素的属性往下找。
find_element_by_xpath("//div[@name='name']/input/tc")
#使用逻辑运算符and,当一个属性无法区分一个元素时,我们可以使用and连接更多属性来标识唯一的元素
find_element_by_xpath("//from[@name='name' and @class='class']")
css定位
- id定位
find_element_by_css_selector("#id")
- classname定位
find_element_by_css_selector(".classname")
- 标签定位
find_element_by_css_selector("div")
- 父元素层级定位(父元素下面所有的input)
find_element_by_css_selector("div>input")
- 属性定位(可以是如何属性但必须唯一)
find_element_by_css_selector("[name='name']")
- 组合定位
find_element_by_css_selector("div#id>input.classname")