from selenium import webdriver import time driver=webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(3) #css通过id属性定位 driver.find_element_by_css_selector("#kw").send_keys("python") #css通过class属性定位 driver.find_element_by_css_selector(".s_ipt").send_keys("python") #css通过标签属性定位 driver.find_element_by_css_selector("input").send_keys("python")
from selenium import webdriver import time driver=webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(3) #css通过name属性定位 driver.find_element_by_css_selector("[name=‘wd‘]").send_keys("python") #css通过autocomplete属性定位 driver.find_element_by_css_selector("[autocomplete=‘off‘]").send_keys("python") #css还可以通过type属性定位
from selenium import webdriver import time driver=webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(3) #css通过标签与class属性定位 driver.find_element_by_css_selector("input.s_ipt").send_keys("python") #css通过标签与id属性定位 driver.find_element_by_css_selector("input#kw").send_keys("python") #css通过标签与id属性定位 driver.find_element_by_css_selector("input[id=‘kw‘]").send_keys("python") #可以只要属性定位,没有值,但是此处有同名,运行会报错 driver.find_element_by_css_selector("input[name]") #css通过两种属性组合定位 driver.find_element_by_css_selector("[name=‘wd‘][autocomplete=‘off‘]").send_keys("python")
from selenium import webdriver import time driver=webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(3) #css通过层级id属性的定位 driver.find_element_by_css_selector("form#form>span>input").send_keys("python") #css通过层级class属性定位 driver.find_element_by_css_selector("form.fm>span>input").send_keys("python")
nth-last-child(n),倒数第几个标签
first-child,第一个标签
last-child,最后一个标签
only-child,唯一的标签
from selenium import webdriver import time driver=webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(3) driver.find_element_by_css_selector("[name=‘wd‘][autocomplete=‘off‘]").send_keys("python")
from selenium import webdriver import time driver=webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(3) #属性值由多个空格隔开,匹配其中一个值 driver.find_element_by_css_selector("input[class~=‘s_btn‘]") #匹配属性值为字符串开头 driver.find_element_by_css_selector("input[class^=‘s_i‘]").send_keys("python") #匹配属性值为字符串结尾 driver.find_element_by_css_selector("input[class$=‘ipt‘]").send_keys("python")
原文:https://www.cnblogs.com/Arcy/p/11099626.html