selenium webdriver(python)---定位元
2018-05-22 本文已影响15人
太阳当空照我要上学校
下面介绍几种常见的Selenium元素方法。
1)通过ID定位元素

如图1 代码,根据元素id查找元素,可以这样写:
driver.find_element_by_id("kw")
2)通过Name定位元素
如图1 代码,根据元素name查找元素,可以这样写:
driver.find_element_by_name("wd")
3)通过className定位元素
如图1 代码,根据元素className查找元素,可以这样写:
driver.find_element_by_class_name("s_ipt")
4)通过css定位元素

如图2 代码,查找p元素,可以这样写:
driver.find_element_by_css_selector('p.content')
5)通过链接文本定位元素


如图3 代码,根据链接文本查找"网页",可以这样写:
driver.find_element_by_link_text("网页")
6)通过XPATH定位元素

如图4 代码,查找 "username"元素,可以这样写:driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
或
driver.find_element_by_xpath("//input[@name='username']")