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. 基于java代码的Spring-mvc框架配置

     Spring 版本 4.3.2   maven项目 1.首先上项目目录图,主要用到的配置文件,略去css和js的文件 引包: 2.主要代码: (1)NetpageWebAppInitializer类 ...

  2. Ubuntu之root权限的获取

    方案一: Ubuntu的root密码在没有设置之前是随机的,即在每一次开机的时候他的密码都不同,但是由于在安装Ubuntu的时候需要建立一个账户,而这个招呼又属于admin组,因此它可以对root进行 ...

  3. coding.net

    http://coding.net 看上去不错,简洁自然. https://coding.net/u/zhongzf/p/TestProject/git http://zhongzf.coding.i ...

  4. xUtils 1.8.4 (Android工具库) 发布 - http模块优化

    感谢关注xUitls的网友最近一段时间给予的热心反馈,xUtils近期做了很多细节优化,同时修复和优化了大家反馈的一些问题.重要的变化有http请求返回更全面的结果信息:下载设置断点续下时,如果服务器 ...

  5. 小谈 - web模仿手机打电话与正则表达式

    昨天遇到了一个很棘手的问题,就是手机端调用web端的页面,如果用编辑器插入的内容页面中有电话的的数据就要变一下格式,让手机端可以实现拨号的功能. 研究了半天就是没一点头绪,但是偶尔看到数据中每一个电话 ...

  6. 实现tip浮层

    实现简单的tip浮层: html代码: <!doctype html> <html> <head> <meta charset="utf-8&quo ...

  7. 根据Excel的内容和word模板生成对应的word文档

    Sub setname() Dim I As Integer Dim pspname As String Dim pspnumber As String Dim path As String Dim ...

  8. Linux内核TCP/IP参数分析与调优

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0225/167.html?1456482565 如下图展示的是TCP的三个阶段.1,TCP三次握手. ...

  9. Oracle函数脚本记录

    --内置函数 --聚合函数 返回单个值 '; --count()记录条数 select sum(degree) from score t; --sum(degree)求成绩总和 select avg( ...

  10. Node.js入门:前后端模块的异同

        通常有一些模块可以同时适用于前后端,但是在浏览器端通过script标签的载入JavaScript文件的方式与Node.js不同.Node.js在载入到最终的执行中,进行了包装,使得每个文件中的 ...