1110Selenium web自動化測試經驗分享-Xpath定位總結
阿新 • • 發佈:2018-12-20
剛分享過App的實戰,這次分享下Web如何運用Xpath定位。本期的整體思路和App那期是一樣的。
1. 根據 元素屬性名和屬性值 來定位
def test_16(self): """xpath 根據元素屬性名和屬性值來定位""" # 通過元素的id、name、class這屬性定位 driver = webdriver.Chrome() driver.implicitly_wait(5) driver.get("https://www.baidu.com") # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off"> # xpath 通過id屬性來定位 driver.find_element_by_xpath('//*[@id="kw"]').send_keys('python') # xpath 通過name屬性來定位 driver.find_element_by_xpath('//*[@name="wd"]').send_keys('python') # xpath 通過class屬性來定位 driver.find_element_by_xpath('//*[@class="s_ipt"]').send_keys('python') # xpath 也可以通過 其他屬性 定位 driver.find_element_by_xpath('//*[@autocomplete="off"]').send_keys('python') time.sleep(1.5) driver.quit()
def test_16b(self): """xpath 根據元素屬性名和屬性值來定位""" self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(5) self.driver.get("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F") # <input type="text" id="input1" value="" class="input-text" onkeydown="check_enter(event)"> self.xin_find_element(self.driver, By.XPATH, '//*[@id="input1"]').send_keys('python') time.sleep(0.5) self.xin_find_element(self.driver, By.XPATH, '//*[@class="input-text"]').clear() # <input type="password" id="input2" value="" class="input-text" onkeydown="check_enter(event)"> self.xin_find_element(self.driver, By.XPATH, '//*[@type="password"]').send_keys('python') time.sleep(0.5) self.xin_find_element(self.driver, By.XPATH, '//*[@id="input2"]').clear() time.sleep(1) self.driver.quit()
2.根據 標籤 + 元素屬性名和值 來定位
def test_18a(self): """xpath 標籤+ 元素屬性名和屬性值 組合定位""" # 1.有時候同一個屬性,同名的比較多,這時候可以通過標籤篩選下,定位更準一點 # 2.如果不想制定標籤名稱,可以用*號表示任意標籤 # 3.如果想制定具體某個標籤,就可以直接寫標籤名稱 driver = webdriver.Chrome() driver.implicitly_wait(5) driver.get("https://www.baidu.com") # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off"> driver.find_element_by_xpath('//input[@autocomplete="off"]').send_keys('python') driver.find_element_by_xpath('//input[@id="kw"]').send_keys('python') driver.find_element_by_xpath('//input[@name="wd"]').send_keys('python') time.sleep(1.5) driver.quit()
def test_18b(self):
"""xpath 標籤 + 元素屬性名和屬性值 組合定位"""
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://passport.csdn.net/account/login")
driver.find_element_by_link_text('賬號登入').click()
time.sleep(2)
# <input id="username" name="username" tabindex="1" placeholder="輸入使用者名稱/郵箱/手機號" class="user-name" type="text" value="">
driver.find_element_by_xpath('//input[@placeholder="輸入使用者名稱/郵箱/手機號"]').send_keys('python')
time.sleep(0.5)
driver.find_element_by_xpath('//input[@name="username"]').send_keys('6666')
# <input id="password" name="password" tabindex="2" placeholder="輸入密碼" class="pass-word" type="password" value="" autocomplete="off">
driver.find_element_by_xpath('//input[@placeholder="輸入密碼"]').send_keys('python')
time.sleep(0.5)
driver.find_element_by_xpath('//input[@class="pass-word"]').clear()
time.sleep(2)
driver.quit()
3.根據 層級關係+索引 來定位
def test_19a(self):
"""xpath 層級-父定位子"""
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")
# 它爺爺來定位孫子
driver.find_element_by_xpath('//form[@id="form"]/span/input').send_keys('python')
driver.find_element_by_xpath('//input[@id="su"]').click()
time.sleep(2)
driver.quit()
def test_19b(self):
"""xpath 層級關係、 索引"""
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://passport.csdn.net/account/login")
driver.find_element_by_link_text('賬號登入').click()
time.sleep(2)
# 它爺爺來定位孫子、 索引
driver.find_element_by_xpath('//div[@class="user-pass"]/form/input[2]').send_keys('python')
time.sleep(0.5)
# 父定位子、 索引
driver.find_element_by_xpath('//form[@id="fm1"]/input[3]').send_keys('python')
time.sleep(2)
driver.quit()
def test_19c(self):
"""xpath 層級關係、 索引"""
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(5)
self.driver.get("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F")
# 使用者名稱
self.xin_find_element(self.driver, By.XPATH, '//div[@class="block"]/input').send_keys('python') # 父定位子
time.sleep(0.5)
self.xin_find_element(self.driver, By.XPATH, '//form[@method="post"]/div[2]/input').send_keys('python') # 它爺爺來定位、 索引
# 密碼
self.xin_find_element(self.driver, By.XPATH, '//form[@method="post"]/div[3]/input').send_keys('python') # 它爺爺來定位、 索引
time.sleep(0.5)
self.xin_find_element(self.driver, By.XPATH, '//div[@class="block"][2]/input').send_keys('python') # 父定位子、 索引
time.sleep(1)
self.driver.quit()
4.根據 兄弟節點 來定位
def test_20(self):
"""xpath-兄弟節點"""
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://www.baidu.com")
# 兄弟來定位
driver.find_element_by_xpath('//a[@id="quickdelete"]/../input').send_keys('python')
time.sleep(0.5)
driver.find_element_by_xpath('//a[@title="清空" and @class="quickdelete"]/parent::*/input').send_keys('6666')
time.sleep(0.5)
driver.find_element_by_xpath('//a[@id="quickdelete"]/parent::span/input').clear()
time.sleep(1.5)
driver.quit()
def test_20b(self):
"""xpath-兄弟節點"""
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(5)
self.driver.get("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F")
# 使用者名稱
self.xin_find_element(self.driver, By.XPATH, '//span[@id="tip_input1"]/../input').send_keys('python') # 同一父類--兄弟節點
time.sleep(0.5)
self.xin_find_element(self.driver, By.XPATH, '//div[@id="Heading"]/parent::form/div[2]/input').clear() # 同一爺爺類--兄弟節點
# 密碼
self.xin_find_element(self.driver, By.XPATH, '//label[@class="label-line"]/parent::*/input[@id="input2"]').send_keys('python') # 同一父類--兄弟節點
time.sleep(0.5)
self.xin_find_element(self.driver, By.XPATH, '//div[@id="checkWay"]/../div[3]/input').clear() # 同一爺爺類--兄弟節點
time.sleep(1)
self.driver.quit()
def test_20c(self):
"""xpath-兄弟節點"""
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(5)
self.driver.get("https://passport.csdn.net/account/login")
self.xin_find_element(self.driver, By.LINK_TEXT, "賬號登入").click()
time.sleep(2)
# 使用者名稱
self.xin_find_element(self.driver, By.XPATH, '//input[@id="gps"]/../input[2]').send_keys('python')
time.sleep(0.5)
self.xin_find_element(self.driver, By.XPATH, '//div[@class="mobile-auth"]/parent::form/input[2]').clear()
# 密碼
self.xin_find_element(self.driver, By.XPATH, '//input[@id="fkid"]/parent::*/input[3]').send_keys('python')
time.sleep(0.5)
self.xin_find_element(self.driver, By.XPATH, '//input[@id="iframe"]/../input[3]').clear()
# 登入按鈕
self.xin_find_element(self.driver, By.XPATH, '//form[@id="fm1"]/input[9]').click()
time.sleep(1)
self.driver.quit()
5. 根據 邏輯運算 and or not 組合定位
def test_21(self):
"""xpath 支援邏輯運算 and or not"""
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://www.baidu.com")
# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
driver.find_element_by_xpath('//*[@id="kw" and @name="wd"]').send_keys('python')
driver.find_element_by_xpath('//*[@class="s_ipt" and @name="wd"]').send_keys('python')
time.sleep(1)
driver.quit()
def test_21b(self):
"""xpath 支援邏輯運算 and or not"""
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://www.baidu.com")
# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
driver.find_element_by_xpath('//*[@id="kw" or @name="wd"]').send_keys('python')
driver.find_element_by_xpath('//*[@class="s_ipt" or @name="wd"]').send_keys('python')
time.sleep(1)
driver.quit()
6.根據 模糊定位contains、starts-with
def test_22a(self):
"""xpath 模糊匹配 contains"""
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://www.baidu.com")
# <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
driver.find_element_by_xpath('//*[contains(@name,"tj_trhao")]').click()
driver.back()
# <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
driver.find_element_by_xpath('//*[contains(@href,"https://www.hao123")]').click()
driver.back()
# <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新聞</a>
driver.find_element_by_xpath('//*[contains(@class,"mna")]').click()
time.sleep(1)
driver.quit()
def test_22b(self):
"""xpath 模糊匹配 contains"""
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://www.baidu.com")
# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
driver.find_element_by_xpath('//input[contains(@id,"w")]').send_keys('python')
driver.find_element_by_xpath('//input[contains(@name,"w")]').send_keys('python')
driver.find_element_by_xpath('//input[contains(@class,"ipt")]').send_keys('python')
driver.find_element_by_xpath('//input[contains(@autocomplete,"ff")]').send_keys('python')
time.sleep(1)
driver.quit()
def test_23a(self):
"""xpath 模糊匹配 starts-with"""
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://www.baidu.com")
# <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新聞</a>
driver.find_element_by_xpath('//*[starts-with(@class, "mn")]').click()
driver.back()
# <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
driver.find_element_by_xpath('//*[starts-with(@name, "tj_trhao")]').click()
driver.back()
# <a href="https://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
driver.find_element_by_xpath('//*[starts-with(@href, "https://www.hao123")]').click()
time.sleep(1)
driver.quit()
def test_23b(self):
"""xpath 模糊匹配 starts-with"""
driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://www.baidu.com")
# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
driver.find_element_by_xpath('//input[starts-with(@autocomplete,"o")]').send_keys('python')
driver.find_element_by_xpath('//input[starts-with(@id,"k")]').send_keys('python')
driver.find_element_by_xpath('//input[starts-with(@name,"w")]').send_keys('python')
driver.find_element_by_xpath('//input[starts-with(@class,"s_i")]').send_keys('python')
time.sleep(1)
driver.quit()
ends-with是xpath2.0的語法,可能我的瀏覽器還只支援1.0的語法,所以就放棄整理那部分。
交流技術 歡迎+QQ 153132336 zy 歡迎關注 微信公眾號:紫雲小站