码迷,mamicode.com
首页 > 编程语言 > 详细

Python+Selenium练习篇之6-利用class name定位元素

时间:2018-12-22 20:36:58      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:baidu   port   mat   quit   for   orm   except   fir   页面   

有时候,我们在用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定位方法。

Python+Selenium练习篇之6-利用class name定位元素

标签:baidu   port   mat   quit   for   orm   except   fir   页面   

原文地址:https://www.cnblogs.com/wangyinghao/p/10162053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!