Selenium+Python定位實例

常見的定位方式參見:http://www.cnblogs.com/ranxf/p/7928732.htmlcss

一、ID定位(find_element_by_id)
html

<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">
 

實現代碼:ide

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")

二、CSS定位(find_element_by_css_selector)ui

頁面標籤:spa

<div id="xz-guide-system" class="xz-content-guide-btpanel-button xz-w-eq-h xz-cursor-hand">button</div>

實現代碼3d

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標籤屬性

三、find_elementscode

 

上面的頁面中多個(div class="panel-title"htm

<div class="panel-title" style="height: 24px; line-height: 24px;">設備</div>

設備屬於第五個,採用CSS和find_elements定位方法以下「blog

device = driver.find_elements("css selector", ".panel-title")
print(device[4].text)
device[4].click()

四、Xpath絕對路徑定位(find_element_by_xpath)element

定位設備管理採用Xpath定位方法以下:

driver.find_element_by_xpath("//*[@id='accordion_child_manage']/div[1]/div[2]/ul/li[2]").click()
相關文章
相關標籤/搜索