Selenium+Python定位实例
常见的定位方式参见:http://www.cnblogs.com/ranxf/p/7928732.html
1、ID定位(find_element_by_id)
<input class="easyui-textbox" id="userID" name="userID" size="29" data-options="required:true" type="text">
<input class="easyui-validatebox validatebox-text" id="password" name="password" value="" size="30" data-options="validType:'password'" type="password">
实现代码:
driver.find_element_by_id("userID").clear() driver.find_element_by_id("userID").send_keys("000000")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("0000000")
2、CSS定位(find_element_by_css_selector)
页面标签:
<div id="xz-guide-system" class="xz-content-guide-btpanel-button xz-w-eq-h xz-cursor-hand">button</div>
实现代码
driver.find_element_by_css_selector("div[id='xz-guide-system']").click() # ok 用标签名称+属性值 driver.find_element_by_css_selector("#xz-guide-system").click() # ok css用#号表示id标签属性
3、find_elements
上面的页面中多个(div class="panel-title")
<div class="panel-title" style="height: 24px; line-height: 24px;">设备</div>
设备属于第五个,采用CSS和find_elements定位方法如下“
device = driver.find_elements("css selector", ".panel-title") print(device[4].text) device[4].click()
4、Xpath绝对路径定位(find_element_by_xpath)
定位设备管理采用Xpath定位方法如下:
driver.find_element_by_xpath("//*[@id='accordion_child_manage']/div[1]/div[2]/ul/li[2]").click()