Selenium webdriver是完全模拟用户在对浏览器进行操作,所有用户都是在页面进行的单击、双击、输入、滚动等操作,而webdriver也是一样,所以需要我们指定元素让webdriver进行单击、双击、输入等操作,所以元素定位是UI自动化测试的前提条件。
selenium总共有八种定位方法
driver.find_elements_by_id()
driver.find_element_by_name()
driver.find_element_by_tag_name()
driver.find_element_by_class_name()
driver.find_element_by_link_text()
driver.find_element_by_partial_link_text()
driver.find_element_by_xpath()
driver.find_element_by_css_selector()
其中xpath与css为最常用的定位方式,本文章以百度搜索框为例重点介绍css定位的使用(python版本)
from selenium import webdriver driver = webdriver.Chrome() driver.get("https://www.baidu.com") # 1)绝对路径(一般不推荐使用,此处不介绍) # 2)id选择器 # driver.find_element_by_css_selector("#kw").send_keys("ethon") # 3)class选择器 # driver.find_element_by_css_selector(".s_ipt").send_keys("ethon") # 4)其他属性定位 # driver.find_element_by_css_selector("[autocomplete='off']").send_keys("ethon") # 5)通过部分属性定位 # * 包含某个字符 # ^ 以某个字符开关 # $ 以某个字符结尾 # driver.find_element_by_css_selector("[autocomplete*='f']").send_keys("ethon") # driver.find_element_by_css_selector("[autocomplete^='o']").send_keys("ethon") # driver.find_element_by_css_selector("[autocomplete$='f']").send_keys("ethon") # 6)通过层级定位 # driver.find_element_by_css_selector("form>span>input").send_keys("ethon") # driver.find_element_by_css_selector("form#form>span>input").send_keys("ethon") # 层级与id组合定位 # driver.find_element_by_css_selector("form.fm>span>input").send_keys("ethon") # 层级与class组合定位 # 7)通过兄弟节点定位 # driver.find_element_by_css_selector("div#u1>a:first-child").click() # driver.find_element_by_css_selector("div#u1>a:nth-child(2)").click() driver.find_element_by_css_selector("div#u1>a:last-child").click()
CSS定位的简单场景
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>单选框</h2> <div id="s_radio"> <input type="radio" name="course" value="Java">Java<br/> <input type="radio" name="course" value="Python">Python<br/> <input type="radio" name="course" value="MySQL" checked="checked">MySQL<br/> </div> <h2>复选框</h2> <div id="s_checkbox"> <input type="checkbox" name="course" value="Java"/>Java<br/> <input type="checkbox" name="course" value="Python"/>Python<br/> <input type="checkbox" name="course" value="MySQL" checked="checked"/>MySQL<br/> </div> <h2>下拉框</h2> <select id="s_select"> <option value="Java">Java</option> <option value="Python">Python</option> <option value="MySQL">MySQL</option> </select> </body> </html>
# 单选框的定位 driver = webdriver.Chrome("D:\\chromedriver.exe") driver.get("D:\\test.html") # s_radio input[value="Python"] 表示id为s_radio,子级为input元素且value属性值为Python driver.find_element_by_css_selector('#s_radio input[value="Python"]').click()
#复选框的定位 driver = webdriver.Chrome("D:\\chromedriver.exe") driver.get("D:\\test.html") elements = driver.find_elements_by_css_selector('#s_checkbox input[checked="checked"]') # 取消默认选择的数据 for element in elements: element.click() driver.find_element_by_css_selector('#s_checkbox input[value="Java"]').click() driver.find_element_by_css_selector('#s_checkbox input[value="Python"]').click()
#下拉框的定位 from selenium.webdriver.support.select import Select driver = webdriver.Chrome("D:\\chromedriver.exe") driver.get("D:\\test.html") #以索引方式定位 Select(driver.find_element_by_id("s_select")).select_by_index("1") #以value值方式定位 Select(driver.find_element_by_id("s_select")).select_by_value("Python") #以文本值方式定位 Select(driver.find_element_by_id("s_select")).select_by_visible_text("Python")