selenium 中xpath定位的幾種方式
阿新 • • 發佈:2018-04-07
utf 文本 webdriver 元素 ins send 失敗 ima ive
#-*-coding:utf-8-*-
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
url = "https://www.baidu.com/"
# url = "https://home.cnblogs.com/u/tDayUp/"
driver.get(url)
"""通過tag *號匹配標簽定位 一 """
# driver.find_element_by_xpath("//*[@id =‘kw‘]").send_keys(u"博客園")
# driver.find_element_by_xpath("//*[@autocomplete =‘off‘]").send_keys(u"博客園")
# driver.find_element_by_xpath("//*[@class =‘s_ipt‘]").send_keys(u"博客園")
# driver.find_element_by_xpath("//*[@name =‘wd‘]").send_keys(u"博客園")
"""通過指定標簽名稱定位 二 """
# driver.find_element_by_xpath("//input[@id =‘kw‘]").send_keys(u"博客園")
# driver.find_element_by_xpath("//input[@autocomplete =‘off‘]").send_keys(u"博客園")
# driver.find_element_by_xpath("//input[@class =‘s_ipt‘]").send_keys(u"博客園")
# driver.find_element_by_xpath("//input[@name =‘wd‘]").send_keys(u"博客園")
"""組合屬性定位元素 多個元素共同定位 三"""
# driver.find_element_by_xpath("//input[@id=‘kw‘ and @name=‘wd‘]").send_keys(u"博客園")
# driver.find_element_by_xpath("//input[@id=‘kw‘ or @name=‘wd‘]").send_keys(u"博客園")
"""絕對路徑定位:手寫失敗, 一般也不會用到 四"""
# driver.find_element_by_xpath("//#wrapper/#head.head_wrapper/.s_form/.s_form_wrapper/.fm/.bg/.s_ipt").send_keys(u"博客園")
"""xpath 文本定位 五"""
# driver.find_element_by_xpath(".//*[text()=‘新聞‘]").click()
# print driver.find_element_by_xpath(".//*[text()=‘新聞‘]").text
"""層級定位 六"""
# driver.find_element_by_xpath("//form[@id =‘form‘]/span/input").send_keys(u"博客園")
"""通過索引定位 七"""
# dj = driver.find_element_by_xpath(".//*[@id=‘u1‘]/a[8]")
# ActionChains(driver).move_to_element(dj).perform()
# sleep(2)
# #選擇設置---搜索設置
# djxz = driver.find_element_by_link_text("搜索設置")
# ActionChains(driver).click(djxz).perform()
# sleep(2)
# yssz = driver.find_element_by_xpath(".//*[@id=‘nr‘]")
# ActionChains(driver).click(yssz).perform()
## 通過索引定位
# ysszxz = driver.find_element_by_xpath(".//*[@id=‘nr‘]/option[2]")
# ActionChains(driver).click(ysszxz).perform()
"""通過父層級查找八 失敗------"""
# driver.find_element_by_xpath("*[@id =‘homepage1_HomePageDays_ctl00_ImageLink‘]/../../../../../..").click()
"""模糊匹配 九"""
# #文字模糊
# driver.find_element_by_xpath("//a[contains(text(),‘新‘)]").click()
# #屬性匹配
# driver.find_element_by_xpath("//input[contains(@id,‘s_btn_wr‘)]").click()
sleep(2)
driver.quit()
selenium 中xpath定位的幾種方式