1. 程式人生 > >Selenium3+webdriver學習筆記3(xpath方式元素定位)

Selenium3+webdriver學習筆記3(xpath方式元素定位)

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from selenium import webdriver

import time,os

# about:addons 火狐瀏覽器安裝元件,訪問的地址

# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#id
keys="selenium自動化"
# url="https://www.baidu.com/"
# url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/select01.html"
url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/a03.html"
driver=webdriver.Firefox()

driver.get(url)

#id name class 屬性定位
# driver.find_element_by_xpath("//*[@id='kw']").send_keys(keys)
# driver.find_element_by_xpath("//*[@name='wd']").send_keys(keys)
# driver.find_element_by_xpath("//*[@class='s_ipt']").send_keys(keys)

#其他屬性定位
# driver.find_element_by_xpath("//*[@autocomplete='off']").send_keys(keys)

#指定標籤名稱 寫標籤名稱,不指定則寫 *,如搜尋框 input
# driver.find_element_by_xpath("//input[@class='s_ipt']").send_keys(keys)

#通過多級層級來定位 ,上一個層級 上上層級
# <form name="f" id="form" action="/s" class="fm" onsubmit="javascript:F.call('ps/sug','pssubmit');">
# <span id="s_kw_wrap" class="bg s_ipt_wr quickdelete-wrap">
# <span class="soutu-btn"></span><input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">

# driver.find_element_by_xpath("//form[@id='form']/span/input").send_keys(keys)

#select01.html檔案
# 下拉框選擇形式
# <select id="status" class="form-control valid" onchange="" name="status">
# <option value=""></option>
# <option value="0">未稽核</option>
# <option value="1">初審通過</option>
# <option value="2">複審通過</option>
# <option value="3">稽核不通過</option>
# </select>

# driver.find_element_by_xpath("//option[@value='2']").click()

# <a href="https://www.hao123.com" target="_blank" class="mnav">hao123</a>
# 模糊匹配
# driver.find_element_by_xpath("//*[contains(text(),'hao123')]").click()

# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#模糊匹配 包含屬性
# driver.find_element_by_xpath("//*[contains(@id,'kw')]").send_keys(keys)

#模糊匹配 已什麼開頭
# driver.find_element_by_xpath("//input[starts-with(@class,'s_')]").send_keys(keys)

# find_element方式 單個數據
# driver.find_element(by='id',value='kw').send_keys(keys)

# a03.html檔案
# <div id="u_sp" class="s-isindex-wrap s-sp-menu">
# <a href="http://news.baidu.com" target="_blank" class="mnav">新聞</a>
# <a href="https://www.hao123.com" target="_blank" class="mnav">hao123</a>
# <a href="http://map.baidu.com" target="_blank" class="mnav">地圖</a>
# <a href="http://v.baidu.com" target="_blank" class="mnav">視訊</a>
# <a href="http://tieba.baidu.com" target="_blank" class="mnav">貼吧</a>
# <a href="http://xueshu.baidu.com" target="_blank" class="mnav">學術</a>
# <a id="s_username_top" class="s-user-name-top" data-tid="2004" href="http://i.baidu.com/" target="_blank"></a></div>

#find_elements方式 多個數據
# len1=driver.find_elements_by_xpath("//a[@class='mnav']")
# len2=driver.find_elements(by='class name',value='mnav')
# print(len(len1))
# print(len(len2))

time.sleep(3)
driver.close()