使用python selenium webdriver模拟浏览器
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模拟浏览器的更多相关文章
- python selenium webdriver处理浏览器滚动条
用键盘右下角的UP,DOWN按键来处理页面滚动条 这种方法很灵活用起来很方便!!!! from selenium import webdriver import time from selenium. ...
- Python Selenium Webdriver常用方法总结
Python Selenium Webdriver常用方法总结 常用方法函数 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() 最大化窗口: m ...
- Python+Selenium自动化 模拟鼠标操作
Python+Selenium自动化 模拟鼠标操作 在webdriver中,鼠标的一些操作如:双击.右击.悬停.拖动等都被封装在ActionChains类中,我们只用在需要使用的时候,导入这个类就 ...
- selenium WebDriver 对浏览器标签页的切换
关于selenium WebDriver 对浏览器标签页的切换,现在的市面上最新的浏览器,当点击一个链接打开一个新的页面都是在浏览器中打开一个标签页,而selenium只能对窗口进行切换的方法,只能操 ...
- Python+Selenium自动化-模拟键盘操作
Python+Selenium自动化-模拟键盘操作 0.导入键盘类Keys() selenium中的Keys()类提供了大部分的键盘操作方法:通过send_keys()方法来模拟键盘上的按键. # ...
- Python+Selenium自动化-设置浏览器大小、刷新页面、前进和后退
Python+Selenium自动化-设置浏览器大小.刷新页面.前进和后退 1.设置浏览器大小 maximize_window():设置浏览器大小为全屏 set_window_size(500,5 ...
- Python使用mechanize模拟浏览器
Python使用mechanize模拟浏览器 之前我使用自带的urllib2模拟浏览器去进行訪问网页等操作,非常多站点都会出错误,还会返回乱码.之后使用了 mechanize模拟浏览器,这些情况都没出 ...
- Python+Selenium+webdriver环境搭建(windows)以及相关资源下载链接
今天记录一下测试小菜鸟alter在测试入门的一点关于python+Selenium+webdriver环境搭建的经历以及资源分享.欢迎交流学习,批评指正. 一.Python的下载与安装 1.pytho ...
- python selenium webdriver入门基本操作
python selenium webdriver入门基本操作 未经作者允许,禁止转载! from selenium import webdriver import time driver=webdr ...
随机推荐
- .net core 中环境变量的配置
配置文件: Properties目录下的launchSettings.json IISExpress配置 "ASPNET_ENV": "EnvironmentOne&qu ...
- 用JS识别各版本浏览器
自昨天发了各浏览器内核介绍的随笔,就闲不住了,想直接写个JS来识别用户所用浏览器版本. 写着写着却发现很多坑爹的地方,比如IE10-的版本是依循常规支持attachEvent,但到了IE11,却只支持 ...
- 图解集合2:LinkedList
初识LinkedList 上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链表.链表原先是C/C++的概念,是 ...
- HTML5 history API实践
一.history API知识点总结 在HTML4中,我们已经可以使用window.history对象来控制历史记录的跳转,可以使用的方法包括: history.forward();//在历史记录中前 ...
- Windows进程通信 -- 共享内存(1)
共享内存的方式原理就是将一份物理内存映射到不同进程各自的虚拟地址空间上,这样每个进程都可以读取同一份数据,从而实现进程通信.因为是通过内存操作实现通信,因此是一种最高效的数据交换方法. 共享内存在 W ...
- hibernate HQL和Criteria
package com.test; import java.util.Date; import java.util.List; import org.hibernate.Query; import o ...
- 缓存篇(Cache)~第二回 使用static静态成员实现服务器端缓存(导航面包屑)~续
返回目录 在写完首篇cache文章后,有些朋友给我来信,希望可以使用JS的方法来实现这个导航功能,所以,才有了这篇文章,JS与C#不同,它虽然有引用类型,但它不具备一些引用类型的特性,如它的方法参数为 ...
- paip.mysql 性能跟iops的以及硬盘缓存的关系
paip.mysql 性能跟iops的以及硬盘缓存的关系 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.c ...
- 《Node.js实战(双色)》作者之一——吴中骅访谈录
- golang开发缓存组件
代码地址github:cache 花了一天时间看了下实验楼的cache组件,使用golang编写的,收获还是蛮多的,缓存组件的设计其实挺简单的,主要思路或者设计点如下: 全局struct对象:用来做缓 ...