#-*-coding:utf-8-*-from selenium import webdriverfrom time import sleepfrom 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()
原文地址:https://www.cnblogs.com/tDayUp/p/8729958.html
时间: 2024-10-09 20:57:46