[Selenium3+python3.6]自动化测试3-八种元素元素定位(Firebug和firepath)
Posted on 2017-03-17 11:57 carol2000 阅读(599) 评论(0) 编辑 收藏 举报参考http://www.cnblogs.com/yoyoketang/p/6123890.html
#coding=utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.baidu.com")
#<input id="kw" class="s_ipt" type="text" autocomplete="off" maxlength="100" name="wd">
#search according id
#Input Pass: find_element_by_id + send_keys
#driver.find_element_by_id('kw').send_keys('python')
#Input Pass:find_element_by_class_name + send_keys
#driver.find_element_by_class_name('s_ipt').send_keys('python2')
#Input Fail:find_elements_by_name
#driver.find_elements_by_name('s_ipt').send_keys('python2')
#because multi items return as list:
if isinstance(driver.find_elements_by_name('wd'),list):
print(driver.find_elements_by_name('wd'))
driver.find_elements_by_name('wd')[0].send_keys("find_elements_by_name")
#Click Pass:find_element_by_link_text + click
#<a class="mnav" target="_blank" href="http://www.hao123.com">hao123</a>
#Note: hao123 an be used for find_element_by_link_text,use ao123 will get ERROR!
#driver.find_element_by_link_text("hao123").click()
#Note: ONLY ao123 an be used for find_elements_by_partial_link_text, but the return is List.
#driver.find_elements_by_partial_link_text("ao123")[0].click()
#!!Input Pass:!!Importanc: find_element_by_xpath
#Install Firefox add-on : firebug, then isntall firepath, use FirePath> switch to CSS get
#driver.find_element_by_xpath(".//*[@id='kw']").send_keys("python3")
#click Pass:find_elements_by_css_selector,use FirePath> switch to CSS get
#Note: find_elements_by_css_selector return is list
driver.find_elements_by_css_selector(".soutu-btn")[0].click()