Python+Selenium练习篇之6-利用class name定位元素
2017-03-31 16:41
591 查看
有时候,我们在用firepath(不会的请点这里)查看元素的XPath信息,发现没有可以用来定位的id信息,这个时候我们就需要考虑用其他的可用的来定位元素。本文介绍如何通过元素节点中class
name的值来定位页面元素。还是以百度首页,搜索输入框定位举例:
XPath截图
相关脚本代码如下:
# coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
try:
driver.find_element_by_class_name("s_ipt")
print ('test pass: element found by class name')
except Exception as e:
print ("Exception found", format(e))
driver.quit()
意见:很多情况下,class利用要比id多,如果class中出现了太长的字符,和可变化的数字,那么请回到用XPath定位方法。
name的值来定位页面元素。还是以百度首页,搜索输入框定位举例:
XPath截图
相关脚本代码如下:
# coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
try:
driver.find_element_by_class_name("s_ipt")
print ('test pass: element found by class name')
except Exception as e:
print ("Exception found", format(e))
driver.quit()
意见:很多情况下,class利用要比id多,如果class中出现了太长的字符,和可变化的数字,那么请回到用XPath定位方法。
相关文章推荐
- Python+Selenium练习篇之2-利用ID定位元素
- Python+Selenium练习篇之4-利用link text定位元素
- Python+Selenium练习篇之7-利用name定位元素
- Python+Selenium练习篇之3-利用tag name定位元素
- Python+Selenium利用link text定位元素
- selenium python (二) 元素定位方法
- selenium2.0关于python的常用函数(二) 定位元素,解析页面时用到
- 轻松自动化---selenium-webdriver(python) (四)--如何定位一组元素?
- Python+selenium定位不到元素的问题及解决方案
- selenium + python之元素定位
- Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
- 使用selenium+python;在页面A点击一个按钮跳转到页面B,页面A没有关闭,浏览器打开了一个B,切换窗口定位页面B的元素
- selenium2+python自动化2-元素定位
- selenium + python之元素定位
- selenium webdriver python 元素定位
- selenium_webdriver(python)多层框架/窗口中元素定位问题
- python-day0003-搭建基于python的selenium自动化测试---元素定位
- [译]Selenium Python文档:四、元素定位