Webdriver从csv中获取xpath

来源:互联网 发布:淘宝店铺卖什么最好卖 编辑:程序博客网 时间:2024/04/30 12:48

最近想用python写一个自动化小工具。

为了便于维护,想把xpath写到文件里,从文件里读取。

早上就开始尝试了。

#coding=utf-8import csvdef zidian():my_file= 'E:\\test\\aa.csv'data=csv.reader(file(my_file,'rb'))for user in data:print user#print user[0]#print user[1]#arry=tuple(user)#for info in user:#                        print infoi=0j=1while j < len(user):print "the value is %s,%s" %(user[i],user[j])i=j+1j=i+1##zidian()

出个如下的错:

Error: line contains NULL byte

只能用度娘了,找到一篇不错的文章:

http://yxmhero1989.blog.163.com/blog/static/112157956201326103812783/

解决方案:出错原因是直接是把后缀为xls的execl文件重命名为csv的 正常的要是另存为csv文件 就不会报错了

譬如我们有这么个csv文件:

使用Python读取和写入CSV文件 - InSun - Minghacker is Insun

 #!/usr/bin/env python

# -*- coding:utf-8 -*-


import csv

with open('egg.csv','rb') as f:
reader = csv.reader(f)
for row in reader:
print row


打印出来是这样的list
['a', '1', '1', '1']
['a', '2', '2', '2']
['b', '3', '3', '3']
['b', '4', '4', '4']
['b', '5', '5', '5']
['b', '6', '6', '6']
['c', '7', '7', '7']
['c', '8', '8', '8']
['c', '9', '9', '9']
['c', '10', '10', '10']
['d', '11', '11', '11']
['e', '12', '12', '12']
['e', '13', '13', '13']
['e', '14', '14', '14']

2.用python写入并生成csv

#!/usr/bin/env python
# -*- coding:utf-8 -*-


import csv

with open('egg2.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['a', '1', '1', '2', '2'])
spamwriter.writerow(['b', '3', '3', '6', '4'])
spamwriter.writerow(['c', '7', '7', '10', '4'])
spamwriter.writerow(['d', '11','11','11', '1'])
spamwriter.writerow(['e', '12','12','14', '3'])


使用Python读取和写入CSV文件 - InSun - Minghacker is Insun
 
这样存进去的是存到一列了 跟我们原本意图存进5列不一样

使用python的csv生成excel所兼容的csv文件的话,主要就是创建writer时的参数时要有dialect=’excel’
#!/usr/bin/env python

# -*- coding:utf-8 -*-


import csv

with open('egg2.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile,dialect='excel')
spamwriter.writerow(['a', '1', '1', '2', '2'])
spamwriter.writerow(['b', '3', '3', '6', '4'])
spamwriter.writerow(['c', '7', '7', '10', '4'])
spamwriter.writerow(['d', '11','11','11', '1'])
spamwriter.writerow(['e', '12','12','14', '3'])




使用Python读取和写入CSV文件 - InSun - Minghacker is Insun
 
这回符合我们要求了。
3.实战:
http://zhidao.baidu.com/question/529653801.html?push=1&group=1

想用python处理一下csv格式的数据

想统计a的第一例的最小的数,第二列最小值,第三列最大值,且a有几个

同样得到b,c,d,e等

由 图1 得到 图2(最后一列为啊,a,b,c,d,e的个数)

使用Python读取和写入CSV文件 - InSun - Minghacker is Insun

使用Python读取和写入CSV文件 - InSun - Minghacker is Insun


4.参考:
http://zhidao.baidu.com/question/529653801.html?push=1&group=1
http://docs.python.org/2/library/csv.html
使用Python读取/导出(写入)CSV文件
http://www.crifan.com/python_read_write_csv_file/
http://webcache.googleusercontent.com/search?q=cache:http://appsmth.appspot.com/smth/subject/python/90604
csv模块“line contains NULL byte”错误解决方案 - [python]
http://no12.blogbus.com/logs/19102665.html
Python中生成(写入数据到)Excel文件中
http://www.crifan.com/export_data_to_excel_file_in_python/
Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据
http://www.crifan.com/python_append_new_data_into_existing_excel_xls_file/

接着我想把文件存成字典形式的,但是怎么将列表转换成字典,费了好大周折,

看了这两篇文章:

http://www.cnblogs.com/linjiqin/p/3674356.html

http://blog.csdn.net/business122/article/details/7536991

发现字典转变不了,继续列表,代码如下:

#coding=utf-8from selenium import webdriverimport csvdef zidian():my_file= 'E:\\test\\aa.csv'data=csv.reader(file(my_file,'rb'))for user in data:print user#print user[0]#print user[1]#arry=tuple(user)#for info in user:#                        print infoprint "the value is %s,%s" %(user[0],user[1])driver = webdriver.Firefox()url = "http://www.baidu.com"driver.get(url)element=driver.find_element_by_id("r'%user[0]'")element.send_keys("r'%user[1]'")driver.find_element_by_id("su1").click()driver.quit()#i=0#j=1'''while j < len(user):print "the value is %s,%s" %(user[i],user[j])i=j+1j=i+1driver = webdriver.Firefox()url = "http://www.baidu.com"driver.get(url)element=driver.find_element_by_id(user[i])element.send_keys(user[j])driver.find_element_by_id("su1").click()driver.quit()'''##zidian()
但是老遇到问题,Python: Error: Inconsistent indentation detected!

找到了偏文章:http://blog.csdn.net/zhenyu5211314/article/details/18220689

在使用Python自带的IDLE编写代码的过程中,经常会出现莫名其妙的错误,比如这个:


说明你的空格和Tab缩进混淆了(Python对缩进非常严格),解决办法也非常之简单:全选代码——Alt+F5(菜单栏Format——点击Tabify Region),解决


解决这个问题后,发现传参不太对,写了个小的来测试下,

#coding=utf-8from selenium import webdriverid="kw1"print "the value is %s" %(id)driver = webdriver.Firefox()url = "http://www.baidu.com"driver.get(url)element=driver.find_element_by_id("kw1")element.send_keys("pyton")driver.find_element_by_id("su1").click()driver.quit()

还是没解决,都是坑啊,先休息,下次弄。



0 0
原创粉丝点击