前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium元素定位

Selenium元素定位

作者头像
Altumn
发布2019-10-21 16:39:22
1.1K0
发布2019-10-21 16:39:22
举报

Selenium常用的8种元素基本定位方式

find_element_by_id() find_element_by_name() find_element_by_class_name() find_element_by_tag_name() find_element_by_link_text() find_element_by_partial_link_text() find_element_by_xpath() find_element_by_css_selector() 在这里将对各种元素定位方式统一使用百度首页进行示例,详细操作步骤有以下内容: 1.打开浏览器,进入百度首页(www.baidu.com); 2.百度页面输入关键字 www.testclass.cn 进行搜索; 3.关闭浏览器; 首先,通过前端工具(火狐浏览器的Firebug)或者直接按F12进入开发者模式查看具体前端代码:

find_element_by_id()

#coding=utf-8
#www.testclass.cn
#Altumn
#2018-11-13
from selenium import webdriverimport timedriver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://www.baidu.com")driver.find_element_by_id("kw").send_keys("www.testclass.cn")#通过id属性定位输入框;
driver.find_element_by_id("su").click()#通过id属性定位“百度一下”查询按钮;

time.sleep(2)
driver.quit()

find_element_by_name()

#coding=utf-8
#www.testclass.cn
#Altumn
#2018-11-13
from selenium import webdriverimport timedriver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://www.baidu.com")driver.find_element_by_name("wd").send_keys("www.testclass.cn")#通过name属性定位输入框;
driver.find_element_by_id("su").click()time.sleep(2)
driver.quit()

find_element_by_class_name()

#coding=utf-8
#www.testclass.cn
#Altumn
#2018-11-13
from selenium import webdriverimport timedriver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("http://www.baidu.com")driver.find_element_by_name("wd").send_keys("www.testclass.cn")
driver.find_element_by_class_name("s_btn").click()#通过class name属性定位“百度一下”查询按钮;
time.sleep(2)
driver.quit()

以下几种定位方式通过定位下图input标签的输入框示例:

find_element_by_tag_name

driver.find_element_by_tag_name("input")#通过tag name属性定位input输入框;

find_element_by_xpath()

通过xpath定位,xpath定位有N种写法,这里列几个常用写法:

driver.find_element_by_xpath("//*[@id='kw']")
driver.find_element_by_xpath("//*[@name='wd']")
driver.find_element_by_xpath("//input[@class='s_ipt']")
driver.find_element_by_xpath("/html/body/form/span/input")
driver.find_element_by_xpath("//span[@class='soutu-btn']/input")
driver.find_element_by_xpath("//form[@id='form']/span/input")
driver.find_element_by_xpath("//input[@id='kw' and @name='wd']")

xpath定位,具有[相对路径][据对路径]的区别: 相对路径:即相对于上下文节点的路径; 绝对路径:即从根目录开始的完整的路径;

driver.find_element_by_xpath('//input[@id="kw"]').send_keys("www.testclass.cn")#相对路径
driver.find_element_by_xpath('//input[@name="wd"]').send_keys("www.testclass.cn")#相对路径
driver.find_element_by_xpath("//input[@id='kw' and @name='wd']").send_keys("www.testclass.cn")#相对路径
driver.find_element_by_xpath('//*[@id="kw"]').send_keys("by_xpath相对路径") #相对路径
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input').send_keys("www.testclass.cn")#绝对路径
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input[@id="kw"]').send_keys("www.testclass.cn")#绝对路径

find_element_by_css_selector()

通过css定位,css定位有N种写法,这里列几个常用写法:

driver.find_element_by_css_selector("#kw")
driver.find_element_by_css_selector("[name=wd]")
driver.find_element_by_css_selector(".s_ipt")
driver.find_element_by_css_selector("html > body > form > span > input")
driver.find_element_by_css_selector("span.soutu-btn> input#kw")
driver.find_element_by_css_selector("form#form > span > input")

css定位,同样具有[相对路径][据对路径]的区别: 相对路径:即相对于上下文节点的路径; 绝对路径:即从根目录开始的完整的路径;

#by_css_selector #绝对路径
driver.find_element_by_css_selector('html>body>div>div>div>div>div>form>span>input[id="kw"]').send_keys("www.testclass.cn")
#绝对路径#by_css_selector #相对路径
driver.find_element_by_css_selector('input[id="kw"]').send_keys("www.testclass.cn")#相对路径
driver.find_element_by_css_selector("#kw").send_keys("www.testclass.cn")#相对路径

find_element_by_link_text()

通过find_element_by_link_text()定位下图百度首页上的一组文本链接。

find_element_by_link_text()

driver.find_element_by_link_text("新闻").click()
driver.find_element_by_link_text("网页").click()

find_element_by_partial_link_text()

driver.find_element_by_partial_link_text("新").click()
driver.find_element_by_partial_link_text("乐").click()
driver.find_element_by_partial_link_text("页").click()

Selenium通过elements复数定位

find_elements_by_name() find_elements_by_class_name() find_elements_by_tag_name() find_elements_by_link_text() find_elements_by_partial_link_text() find_elements_by_xpath() find_elements_by_css_selector() 复数定位方式每次取到的都是具有相同类型属性的一组元素,所以返回的是一个list队列.我们可以通过选择具体第几个元素进行单个元素定位; 百度首页右上角有新闻、hao123、地图、视频、贴吧、学术一些文字链接,查看源码可以发现,这些链接都有共同的class,class=”mnav”。接下来我们通过find_elements_by_class_name()进行元素定位;

详细元素定位代码如下:

#coding=utf-8

#www.testclass.cn
#Altumn
#2018-11-13
from selenium import webdriverimport timedriver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.baidu.com/")
driver.maximize_window()

#定位一组元素;
elements=driver.find_elements_by_class_name("mnav")
print(len(elements))

#循环打印出每个元素的属性值;
for i in range(len(elements)):
   print("第" + str(i) + "个元素")
   print(elements[i].get_attribute("name"))
   print(elements[i].get_attribute("class"))

输出结果如下所示:

PS C:\Users\WangXiao\Desktop\mystuff> cd 'C:\Users\WangXiao\Desktop\mystuff'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\WangXiao\AppData\Local\Programs\Python\Python36\python.exe' 'c:\Users\WangXiao\.vscode\extensions\ms-python.python-2018.7.1\pythonFiles\PythonTools\visualstudio_py_launcher.py' 'C:\Users\WangXiao\Desktop\mystuff' '63291' '34806ad9-833a-4524-8cd6-18ca4aa74f14' 'RedirectOutput,RedirectOutput' 'c:\Users\WangXiao\Desktop\mystuff\elements.py'DevTools listening on ws://127.0.0.1:12164/devtools/browser/06ead576-8801-4f73-8cd9-38ec1678e2d96第0个元素
tj_trnews
mnav
第1个元素
tj_trhao123
mnav
第2个元素
tj_trmap
mnav
第3个元素
tj_trvideo
mnav
第4个元素
tj_trtieba
mnav
第5个元素
tj_trxueshu
mnav

这样你就可以通过元素的属性值判断你要定位的元素:

driver.find_elements_by_class_name("mnav")[0].click()#点击“新闻”;
driver.find_elements_by_class_name("mnav")[1].click()#点击“hao123”;
driver.find_elements_by_class_name("mnav")[2].click()#点击“地图”;
driver.find_elements_by_class_name("mnav")[3].click()#点击“视频”;
driver.find_elements_by_class_name("mnav")[4].click()#点击“贴吧”;
driver.find_elements_by_class_name("mnav")[5].click()#点击“学术”;
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试testclass 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档