Python+Selenium之CSS元素定位(以百度为例)
1 from selenium import webdriver 2 driver = webdriver.Chrome() 3 driver.get("https://www.baidu.com/") 4 # 通过id属性(css属性) 5 driver.find_element_by_css_selector("#kw").send_keys("python") 6 # 通过class属性定位(css属性) 7 driver.find_element_by_css_selector(".s_ipt").send_keys("python") 8 # 通过标签定位(css属性) 9 driver.find_element_by_css_selector("input").send_keys("python") 10 # 通过name属性定位(其他属性) 11 driver.find_element_by_css_selector("[name='wd']").send_keys("python") 12 # 通过autocomplete属性定位(其他属性) 13 driver.find_element_by_css_selector("[autocomplete='off']").send_keys("python") 14 # 通过type属性定位(其他属性) 15 driver.find_element_by_css_selector("[type='text']").send_keys("python") 16 # css也可以通过标签与属性的组合来定位元素 17 driver.find_element_by_css_selector("input.s_ipt").send_keys("python") 18 driver.find_element_by_css_selector("input#kw").send_keys("python") 19 driver.find_element_by_css_selector("input[id='kw']").send_keys("python") 20 # css层级关系 21 driver.find_element_by_css_selector("form#form>span>input").send_keys("python") 22 driver.find_element_by_css_selector("form.fm>span>inout").send_keys("python") 23 # css索引 24 driver.find_element_by_css_selector("select#nr>option:nth-child(1)").click 25 driver.find_element_by_css_selector("select#nr>option:nth-child(2)").click 26 driver.find_element_by_css_selector("select#nr>option:nth-child(3)").click 27 # css逻辑运算 28 driver.find_element_by_css_selector("input[id='kw'][name='wd']").send_keys("python")
本文来自博客园,作者:{Tester十点半},转载请注明原文链接:https://www.cnblogs.com/jialeliu/p/13705774.html