使用get_attribute时,需要注意的地方
2015-04-16 11:37
302 查看
最近,公司在搞web自动化的框架,所以这几天一直在学习python+selenium的东西。在练习定位元素的时候,遇到一个比较奇怪的问题,现在给大家分享一下。希望能够给大家的学习带来帮助。
如上图所示,我的目的是想选择没有售罄的尺码(其中XL是已经售罄的),代码实现如下
#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Ie()
driver.get('http://item.yohobuy.com/product/pro_143453_198737/CLOTTEETaoTouWeiYiCLOTHCA50118.html')//打开网站
time.sleep(3)
element_father = driver.find_element_by_xpath("//*[@id='sizeList']") //获取上一级目录对象
element = element_father.find_elements_by_class_name("left") //获取到一组目的元素
//选中没有售罄的商品尺码
for ele in element:
if ele.get_attribute('num') != 0:
ele.click()
break
else:
continue
一直很奇怪的是,按照上面的实现,永远的是选中第一个XL(已经售罄的),纠结了好久,也调试了很久,才发现get_attribute这个函数获取的属性值是unicode类型,应该算是一个字符串,所以ele.get_attribute('num')取得的元素永远都是不为0,导致选中的永远是已经售罄的“XL”。做如下改动后,就可以正确的执行。
#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Ie()
driver.get('http://item.yohobuy.com/product/pro_143453_198737/CLOTTEETaoTouWeiYiCLOTHCA50118.html')//打开网站
time.sleep(3)
element_father = driver.find_element_by_xpath("//*[@id='sizeList']") //获取上一级目录对象
element = element_father.find_elements_by_class_name("left") //获取到一组目的元素
//选中没有售罄的商品尺码
for ele in element:
if int(ele.get_attribute('num')) != 0: //把取得元素类型转化成int型
ele.click()
break
else:
continue
第一次写,有点忐忑,希望能够给大家带来帮助,谢谢。
如上图所示,我的目的是想选择没有售罄的尺码(其中XL是已经售罄的),代码实现如下
#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Ie()
driver.get('http://item.yohobuy.com/product/pro_143453_198737/CLOTTEETaoTouWeiYiCLOTHCA50118.html')//打开网站
time.sleep(3)
element_father = driver.find_element_by_xpath("//*[@id='sizeList']") //获取上一级目录对象
element = element_father.find_elements_by_class_name("left") //获取到一组目的元素
//选中没有售罄的商品尺码
for ele in element:
if ele.get_attribute('num') != 0:
ele.click()
break
else:
continue
一直很奇怪的是,按照上面的实现,永远的是选中第一个XL(已经售罄的),纠结了好久,也调试了很久,才发现get_attribute这个函数获取的属性值是unicode类型,应该算是一个字符串,所以ele.get_attribute('num')取得的元素永远都是不为0,导致选中的永远是已经售罄的“XL”。做如下改动后,就可以正确的执行。
#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Ie()
driver.get('http://item.yohobuy.com/product/pro_143453_198737/CLOTTEETaoTouWeiYiCLOTHCA50118.html')//打开网站
time.sleep(3)
element_father = driver.find_element_by_xpath("//*[@id='sizeList']") //获取上一级目录对象
element = element_father.find_elements_by_class_name("left") //获取到一组目的元素
//选中没有售罄的商品尺码
for ele in element:
if int(ele.get_attribute('num')) != 0: //把取得元素类型转化成int型
ele.click()
break
else:
continue
第一次写,有点忐忑,希望能够给大家带来帮助,谢谢。
相关文章推荐
- Python的字典和列表的使用中一些需要注意的地方
- C# 使用SuperMap控件需要千万注意的地方(转载)
- 使用SQL Server 2014内存数据库时需要注意的地方
- 记录下串口使用需要注意的地方
- JSP中使用Spring注入的Bean时需要注意的地方
- [js高手之路]使用原型对象(prototype)需要注意的地方
- jQuery Validation Plugin使用过程中需要注意的地方
- 在使用GROUP BY 时候需要注意的地方
- ACE_Message_Queue<ACE_MT_SYNCH>::putq ()使用时需要注意的地方
- javascript中使用css需要注意的地方小结
- 自定义组件中使用枚举类型时需要注意的地方
- iOS 不使用nib文件需要注意的地方
- 需要注意的地方 - const 和typedef的结合使用
- PHP使用memcache长连接作为RPC客户端需要注意的地方
- 使用宏需要注意的地方
- thinkjs项目中使用mongoose需要注意的地方
- 使用sprintf需要注意的地方
- 使用CustomValidator 控件时需要注意的地方
- 使用SQL Server 2014内存数据库时需要注意的地方
- SpringMVC 使用 @PathVariable 需要注意的地方