selenium是进行web自动化测试的一个工具,支持C,C++,Python,Java等语言,他能够实现模拟手工操作浏览器,进行自动化,通过webdriver驱动浏览器操作,我使用的是chrome浏览器,下载chrome  webdriver 放到python的安装目录。

参考连接:

https://pypi.python.org/pypi/selenium

http://selenium-python.readthedocs.io/api.html

http://www.cnblogs.com/fnng/p/3160606.html

from selenium import webdriver
import time
import string
import datetime def usage():
print("*********************************************************************")
print("欢迎使用Amazone差评神器,Enover保留版权,作者:Anker 日期:2016-12-18")
print("*********************************************************************") def genSearchDate():
now = datetime.datetime.now()
print("当前的日期是:%s/%s/%s" % (now.day, now.month, now.year%2000)) #计算当前月的的日期范围
dayarr = []
if now.day <= 10 :
dayarr = [10,1]
elif now.day/10 <= 2:
dayarr = [now.day,10,1]
else:
dayarr = [now.day,20,10,1] #判断是否闰年
day2 = 0
if (now.year%4 == 0 and now.year%100 != 0) or now.year%400 == 0:
day2 = 29
else:
day2 = 28 months=[[0,0],[31,20,10,1,],[day2,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1],[30,20,10,1],[31,20,10,1]] mon=now.month
searchDate=[]
while (mon > 0):
if (mon == now.month):
tmp = dayarr
else:
tmp = months[mon]
for d in range(0,len(tmp)-1):
if d==0:
enddate='%s/%s/%s' % (mon, tmp[d], now.year%2000)
else:
enddate='%s/%s/%s' % (mon, tmp[d]-1, now.year%2000)
begdate='%s/%s/%s' % (mon, tmp[d+1], now.year%2000)
val=[begdate,enddate]
searchDate.append(val)
mon=mon-1
#print(searchDate)
return searchDate #登陆亚马逊
def loginAmazone(driver):
driver.get("https://sellercentral.amazon.com")
driver.find_element_by_id('ap_email').send_keys('xxxxx')
driver.find_element_by_id('ap_password').send_keys('xxxxx')
driver.find_element_by_name('signIn').submit() #设置查询条件 ASIN 和 时间
def searchProcess(driver, asin, begdate,enddate):
driver.get("https://sellercentral.amazon.com/gp/orders-v2/search/ref=ag_myosearch_apsearch_myo")
driver.find_element_by_id('_myoSO_searchTypeSelect').send_keys('ASIN')
driver.find_element_by_id('_myoSO_searchKeyword').send_keys(asin) driver.find_element_by_id('_myoSO_SearchOption_exactDates').click()
driver.find_element_by_id('exactDateBegin').clear()
driver.find_element_by_id('exactDateBegin').send_keys(begdate)
driver.find_element_by_id('exactDateEnd').clear()
driver.find_element_by_id('exactDateEnd').send_keys(enddate) driver.find_element_by_id('_myoSO_SearchButton').click()
time.sleep(2) #设置每页显示50个
def setpage50(driver):
driver.find_element_by_xpath('//option [@value="50"]').click() # click
driver.find_element_by_xpath('//form [@onsubmit="return MYO.LO.DoAjaxSearchCall( this );"]').submit()
time.sleep(2)
driver.find_element_by_id('_myoLO_saveDefaultSearchCheckBox').click() #计算记录个数
def countPage(source):
pattern='</strong> of <strong>'
pos1=source.find(pattern)
beg=pos1+len(pattern)
pos2=source.find('</strong>',pos1+len(pattern))
total=int(source[beg:pos2]) page=total%50
if page==0:
page=total/50
else:
page=int(total/50)+1
print("订单总数为:%s,共计%s页" % (total, page))
return page #翻页 jump to page
def jumppage(driver, page, custid):
rc=False
for index in range(1,page):
print("正在查找第%s页" % index)
elements = driver.find_elements_by_xpath('//input [@maxlength="7"]')
elements[1].find_element_by_xpath('//input [@name="currentPage"]').send_keys(str(index))
driver.find_element_by_id('_myoSO_GoToPageForm_1').submit()
time.sleep(4)
source=driver.page_source
pos=source.find(custid)
if pos != -1:
print('终于找到了,查找记录如下:')
print(source[pos-270:pos+24])
rc=True
break
return rc def searchBadReview(driver, asin, custid, searchDate):
for i in range(0, len(searchDate)):
tmpDate=searchDate[i]
begdate=tmpDate[0]
enddate=tmpDate[1]
print('==============================================')
print("开始找%s到%s的订单" %(begdate, enddate))
searchProcess(driver, asin, begdate, enddate)
setpage50(driver)
source=driver.page_source
page=countPage(source)
rc = jumppage(driver, page, custid)
if rc == True:
break #主函数
def main():
usage() #输入参数
asin = input("请输入ASIN:")
print("你输入的ASIN是: ", asin)
custid = input("请输入Customer profile id:")
print("你输入的内容是: ", custid)
searchDate=genSearchDate()
#print("查找时间范围如下:")
#print(searchDate) #默认浏览器行为
print('==============================================')
print("开始打开浏览器,并登陆Amazone seller center")
driver = webdriver.Chrome()
loginAmazone(driver)
time.sleep(1)
searchBadReview(driver, asin, custid, searchDate)
driver.quit()
time.sleep(60) if __name__ == "__main__":
main()

  

  

使用python selenium webdriver模拟浏览器的更多相关文章

  1. python selenium webdriver处理浏览器滚动条

    用键盘右下角的UP,DOWN按键来处理页面滚动条 这种方法很灵活用起来很方便!!!! from selenium import webdriver import time from selenium. ...

  2. Python Selenium Webdriver常用方法总结

    Python Selenium Webdriver常用方法总结 常用方法函数 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() 最大化窗口: m ...

  3. Python+Selenium自动化 模拟鼠标操作

    Python+Selenium自动化 模拟鼠标操作   在webdriver中,鼠标的一些操作如:双击.右击.悬停.拖动等都被封装在ActionChains类中,我们只用在需要使用的时候,导入这个类就 ...

  4. selenium WebDriver 对浏览器标签页的切换

    关于selenium WebDriver 对浏览器标签页的切换,现在的市面上最新的浏览器,当点击一个链接打开一个新的页面都是在浏览器中打开一个标签页,而selenium只能对窗口进行切换的方法,只能操 ...

  5. Python+Selenium自动化-模拟键盘操作

    Python+Selenium自动化-模拟键盘操作   0.导入键盘类Keys() selenium中的Keys()类提供了大部分的键盘操作方法:通过send_keys()方法来模拟键盘上的按键. # ...

  6. Python+Selenium自动化-设置浏览器大小、刷新页面、前进和后退

    Python+Selenium自动化-设置浏览器大小.刷新页面.前进和后退   1.设置浏览器大小 maximize_window():设置浏览器大小为全屏 set_window_size(500,5 ...

  7. Python使用mechanize模拟浏览器

    Python使用mechanize模拟浏览器 之前我使用自带的urllib2模拟浏览器去进行訪问网页等操作,非常多站点都会出错误,还会返回乱码.之后使用了 mechanize模拟浏览器,这些情况都没出 ...

  8. Python+Selenium+webdriver环境搭建(windows)以及相关资源下载链接

    今天记录一下测试小菜鸟alter在测试入门的一点关于python+Selenium+webdriver环境搭建的经历以及资源分享.欢迎交流学习,批评指正. 一.Python的下载与安装 1.pytho ...

  9. python selenium webdriver入门基本操作

    python selenium webdriver入门基本操作 未经作者允许,禁止转载! from selenium import webdriver import time driver=webdr ...

随机推荐

  1. jquery 获取 scrollHeight

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:匿名用户链接:http://www.zhihu.com/question/20985674/answer/16807177来源 ...

  2. WebView一般用法总结

    下面是webview常规的用法: import android.annotation.SuppressLint;import android.app.Activity;import android.o ...

  3. centos 后台执行C#控制台程序

    1. nohup nohup 无疑是我们首先想到的办法.顾名思义,nohup 的用途就是让提交的命令忽略 hangup 信号.让我们先来看一下 nohup 的帮助信息: NOHUP() User Co ...

  4. 一个空行引起的阿里云负载均衡上部署https证书的问题

    今天在阿里云上购买了WoSign的https证书,在证书签发后,在控制台下载证书文件,一共有2个文件,一个是.key文件(私钥文件),一个是.pem文件(证书文件). 然后在阿里云负载均衡“证书管理” ...

  5. windows设置笔记

    1. 使用Sudo提升权限 http://www.alexblair.org/user-alexblair-post-1046.html 新建 sudo.js 放到 C:\windows\下面,内容如 ...

  6. 我的ORM之五-- 事务

    我的ORM索引 单库事务与分布式事务 单库事务: 性能更好,应用于一个数据库时的场景,当数据库发生变化,如拆分为多个服务器,代码需要修改. 分布式事务:性能相对较差,但有更大的适用场景.当数据库发生变 ...

  7. 用JQ仿造礼德财富网的图片查看器

    现在就职于一家P2P平台,自然也会关注同行其它网站的前端技术,今天要仿造的是礼德内页的一个图片查看器效果.不过说白了,无论人人贷也好礼德财富也好,很多地方的前端都做的不尽如人意,比如忽略细节.缺乏交互 ...

  8. 走进AngularJs(九)表单及表单验证

    年底了越来越懒散,AngularJs的学习落了一段时间,博客最近也没更新.惭愧~前段时间有试了一下用yeoman构建Angular项目,感觉学的差不多了想做个项目练练手,谁知遇到了一系列问题.yeom ...

  9. css position全解析

    1.position:static 所有的元素的默认定位都是position:static,这意味着元素没有被定位,而且在文档中出现在它应该在的位置. 2.position:relative 如果设定 ...

  10. 《OOC》笔记(4)——自动化地将C#代码转化为C代码(结构版)

    <OOC>笔记(4)——自动化地将C#代码转化为C代码(结构版) 我在<C表达面向对象语言的机制——C#版>中已经说明了从C#到C的转换方法.这次看<OOC>也是想 ...