No.9 selenium学习之路之CSS定位
CSS定位方式:
元素中间加“.”表示是class
1.通过ID定位
driver.find_element_by_css_selector("#ID值")
2.通过class定位
driver.find_element_by_css_selector(".s_ipt")
3.通过标签定位
driver.find_element_by_css_selector("input")
4.通过其他属性定位
不局限于所知属性,使用任何属性都可以定位
driver.find_element_by_css_selector("[name='kw']")
driver.find_element_by_css_selector("[autocomplete='off']")
CSS组合定位
1.父子关系
driver.find_element_by_css_selector("span>input")
2.组合定位
driver.find_element_by_css_selector("form.fm>span>input.s_ipt")
driver.find_element_by_css_selector("form#form>span>input#kw")
# 获取文本属性
t = driver.find_element_by_css_selector(".mnav:nth-child(3)").text
# 获取name属性
name = driver.find_element_by_css_selector(".mnav:nth-child(3)").get_attribute("name")
# 获取class属性
class_name = driver.find_element_by_css_selector(".mnav:nth-child(3)").get_attribute("class")
# 获取href
href = driver.find_element_by_css_selector(".mnav:nth-child(3)").get_attribute("href")
# 获取tag
tag = driver.find_element_by_css_selector(".mnav:nth-child(3)").tag_name
# 获取浏览器名称
browser = driver.name
# 获取size
s = driver.find_element_by_css_selector("#kw").size
print s
print type(s)
w = s["width"]
h = s["height"]
print w
print h