1 from selenium import webdriver 2 driver = webdriver.Chrome() 3 driver.get("https://www.baidu.com/") 4 # 用Xpath通过id属性定位 5 # driver.find_element_by_xpath("//*[@id='kw']").send_keys("python") 6 # 用Xpath通过name属性定位 7 # driver.find_element_by_xpath("//*[@name='wd']").send_keys("python") 8 # 用Xpath通过class属性定位 9 # driver.find_element_by_xpath("//*[@class='s_ipt']").send_keys("python") 10 # 用Xpath通过其他属性定位 11 # driver.find_element_by_xpath("//*[@autocomplete='off']").send_keys("python") 12 # 用Xpath通过标签名+属性定位 13 # driver.find_element_by_xpath("//input[@class='s_ipt']").send_keys("python") 14 # 通过定位他爸爸来定位input输入框 15 # driver.find_element_by_xpath("//span[@id='s_kw_wrap']/input").send_keys("python") 16 # 通过定位他爷爷来定位input输入框 17 # driver.find_element_by_xpath("//form[@id='form']/span/input").send_keys("python") 18 # 用Xpath逻辑运算 19 # driver.find_element_by_xpath("//*[@id='kw' and @autocomplete='off']").send_keys("python") 20 # 用Xpath模糊匹配 21 # driver.find_element_by_xpath("//*[contains(text(),'hao123')]").click() 22 # 用Xpath模糊匹配属性 23 # driver.find_element_by_xpath("//*[contains(@id,'kw')]").send_keys("python") 24 # 用Xpath模糊匹配属性以什么开头 25 # driver.find_element_by_xpath("//*[starts-with(@id,'s_kw_')]").click() 26 # 用Xpath模糊匹配属性以什么结尾 27 # driver.find_element_by_xpath("//*[ends-with(@id,'kw_warp')]").click() 28 # 用Xpath正则表达式 29 driver.find_element_by_xpath("//*[matchs(text(),'hao123')]").click()